Warning: Missing argument 2 for wpdb::prepare() on theme functions

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

Read More

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;

Related posts

3 comments

  1. 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 quotes

    $query = "SELECT *,count(*) AS used FROM $wpdb->postmeta 
         WHERE meta_key = '_wp_page_template' AND meta_value = %s 
             GROUP BY meta_value";
    
        $results = $wpdb->get_row($wpdb->prepare($query,$filename),'ARRAY_A'); // Select thrid coloumn accross
    
  2. I 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:

    global $wpdb;
    
    // Old query.  Let's get it ready for prepare...
    // $query = "SELECT *,count(*) AS used FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' AND meta_value = '$filename' GROUP BY meta_value";
    
    // New query, ready for prepare:
    $query = "SELECT *,count(*) AS used FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s GROUP BY meta_value";
    
    // Now we pass that into prepare, along with the two values we want replaced
    $wpdb->prepare($query, '_wp_page_template', $filename);
    
    // And we execute the query...
    $results = $wpdb->get_row($query,'ARRAY_A');
    

    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:

    $post_id = $_POST['post_id'];
    $query = 'SELECT * FROM $wpdb->postmeta WHERE post_id = %d';
    $query = $wpdb->prepare($query, $post_id);
    

    Hopefully this helps!

  3. You need to pass $meta_key as second argument.

    eg:-

    <?php
    // set the meta_key to the appropriate custom field meta key
    $meta_key = 'miles';
    $allmiles = $wpdb->get_var( $wpdb->prepare( 
        "
            SELECT sum(meta_value) 
            FROM $wpdb->postmeta 
            WHERE meta_key = %s
        ", 
        $meta_key
    ) );
    echo "<p>Total miles is {$allmiles}</p>";
    ?> 
    

    so here your second second argument is $filename.

    Reference :- Class Reference / wpdb

Comments are closed.