Execute Update Query In WordPress From functions.php
18,749
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");
}
Author by
Awais Umar
Updated on June 07, 2022Comments
-
Awais Umar almost 2 years
I am trying to execute an update query from my functions.php using init hook.
Here is my code
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.
-
Jerodev over 9 yearsWhere do you set the
$table_name
variable? -
Mike B over 9 yearsblank white page means find the syntax error
-
Awais Umar over 9 years@MikeB There might be a syntax error but I am not getting where could it be. I just have copy pasted the function I am working on. Can you please point out where the syntax error could be? Because as soon as I remove this bit of code from my functions.php, the site starts working again. So whatever is the problem, its in this bit of code...
-
Mike B over 9 years@Symbolwdd You don't need to crowd-source syntax error problems codepad.org/MUpubgwj. Regardless, a lack of error message or feedback STILL means find the error message instead of randomly moving code around to see when it starts kicking again. You could be using a vertical tab somewhere that looks like a space and you'd never know unless you nail down the error.
-