I upgraded my old site from wordpress version 3.4 to 4.4 recently. Suddenly I am getting a PHP warning as per below:
Warning: Missing argument 2 for wpdb::prepare(), called in …../wp-content/themes/…functions/admin-functions.php on line 1543 and defined in …….public/wp-includes/wp-db.php on line 1246
Below are the codes for admin-functions.php:
global $wpdb;
$query = "SELECT *,count(*) AS used FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' AND meta_value = '$filename' GROUP BY meta_value";
$results = $wpdb->get_row($wpdb->prepare($query),'ARRAY_A'); // Select thrid coloumn accross
if(empty($results))
return false;
and wp-db.php
public function prepare( $query, $args ) {
if ( is_null( $query ) )
return;
You need to use placeholders and add the variable as an argument to prepare,
%s
is a placeholder for string value,assuming it is string from your quotesI would recommend reading the documentation for $wpdb->prepare.
Prepare requires at least two arguments to be passed in. The first is the query, using placeholders (
%s
for a string and%d
for a number), and the second is the list of variables / values to place into the query instead of the placeholders.For your specific case, I’m demonstrating in a bit longer format so you can see clearly:
Note that you can use the placeholders for as many values as you want, but the idea is that you use them for any value that is user input. If the value may be posted through a form, for example, you absolutely want to use prepare on that value. Example:
Hopefully this helps!
You need to pass $meta_key as second argument.
eg:-
so here your second second argument is $filename.
Reference :- Class Reference / wpdb