Hello Babiato familly,
I just shared before an SQL request to remove the block of TRP about licence but i was lazy to do it every week, so i adapt it in php and cron job,)
I'm excited to share a little success story with you all today!
Being quite new to the world of coding and WordPress, I often found myself tangled up in repetitive tasks that, while manageable, took up much more time than they should have.
One such task was dealing with a pesky SQL query that I had to run quite frequently across several sites. It was all about cleaning up some license details from various _options tables in the database. While it was not overly complex, doing it manually over and over again became quite a chore!
This is where a brilliant piece of technology stepped in to save the day!
I stumbled upon ChatGPT, an artificial intelligence by OpenAI, and decided to seek its help in automating this task. The goal was simple: transform that manual SQL query into a scheduled task within WordPress, allowing it to run automatically every day without my intervention.
And...it worked!
With the help of ChatGPT, I managed to create a piece of code that I placed in my functions file. Now, the task runs smoothly in the background, eliminating the need for my regular manual intervention. It’s like setting a little robot to work behind the scenes, ensuring I don’t have to worry about those license details anymore.
I will be attaching the code in a file with this message, in case anyone might find it useful or if you are dealing with a similar recurring task. Please feel free to use it, tweak it, or ask any questions about it - I'd be more than happy to share more about this journey!
Thank you, ChatGPT, for the assist and to this community for the continuous learning and sharing opportunities!
Let's keep coding and exploring together!
Please let me if it works for you also!
And if there a better way, upgrade to do,)
PHP:
function execute_my_query() {
global $wpdb;
// Retrieve table names ending with '_options' from the WordPress database
$tables = $wpdb->get_col("SELECT table_name FROM information_schema.tables WHERE table_name LIKE '%_options' AND table_schema = DATABASE()");
$totalDeleted = 0;
// Loop through the tables and delete records where option_name = 'trp_license_details'
foreach($tables as $table){
$deleted = $wpdb->delete( $table, array( 'option_name' => 'trp_license_details' ) );
$totalDeleted += $deleted;
}
// Logging or other action with $totalDeleted if necessary
error_log("Total rows deleted: $totalDeleted");
}
// Hook our function to the cron event
add_action('trp_license_removal', 'execute_my_query');
// Schedule the event if it is not already scheduled
if (!wp_next_scheduled('trp_license_removal')) {
wp_schedule_event(time(), 'daily', 'trp_license_removal');
}