Execute Update Query In WordPress From functions.php

I am trying to execute an update query from my functions.php using init hook.

Here is my code

Read More
add_action('init', 'process_query');
function process_query(){
 $wpdb->query("UPDATE $table_name SET status='inactive' WHERE CURRENT_DATE NOT BETWEEN startdate AND enddate");
}

Now this query works fine when i directly run it on PHPmyadmin’s SQL section. But if i put it to functions.php, the theme goes down and i get a blank webpage.
I have checked WordPress codex syntax for “update” query but it requires arrays. So it won’t work for me in that way.

Related posts

Leave a Reply

1 comment

  1. Use Following Code

    add_action('init', 'process_query');
        function process_query(){
        global $wpdb;
        $table_name = $wpdb->prefix . 'enter_your_table_name_here';
         $wpdb->query("UPDATE $table_name SET status='inactive' WHERE CURRENT_DATE NOT BETWEEN startdate AND enddate");
        }