2
votes

Can anyone help me with this the bottom function is resulting in the error

Warning: Missing argument 2 for wpdb::prepare(), called in /home/.../wp-content/themes/theme/display.php on line 233 and defined in /home/.../wp-includes/wp-db.php on line 1147

This is the function

function ts_display_getnumposts($cat){

    global $wpdb;

    $qryString = "
        SELECT  Count(*) as totpost FROM ".$wpdb->posts." a 
        INNER   JOIN ".$wpdb->term_relationships." b ON a.ID = b.object_id 
        INNER   JOIN ".$wpdb->term_taxonomy." c ON b.term_taxonomy_id = c.term_taxonomy_id
        INNER   JOIN ".$wpdb->terms."  d ON c.term_id = d.term_id
        WHERE   a.post_type = 'display'
    ";

    if(strlen($cat)>0){
        $qryString .= " AND d.slug = '".$cat."'";
    }

    $numposts = $wpdb->get_var($wpdb->prepare($qryString));

    return $numposts;
}

Thank you for reading my question. And thanks in advance for your replies.

1
The error message says you're missing a 2nd argument to the call to the prepare() method. I too do not see a 2nd argument... - Madbreaks

1 Answers

0
votes

Your problem is the prepared part is in a conditional. if strlen($cat) > 0 returns false, you never add it to the query string and therefore have nothing to prepare. Aside that, you also don't pass the $cat to the prepare method, nor do you supply the necessary sprint operator (%s for string, %d for digit, etc). Try something like this:

function ts_display_getnumposts($cat){

    global $wpdb;

    $qryString = "

        SELECT  Count(*) as totpost FROM ".$wpdb->posts." a 

        INNER   JOIN ".$wpdb->term_relationships." b ON a.ID = b.object_id 

        INNER   JOIN ".$wpdb->term_taxonomy." c ON b.term_taxonomy_id = c.term_taxonomy_id

        INNER   JOIN ".$wpdb->terms."  d ON c.term_id = d.term_id

        WHERE   a.post_type = 'display'

    ";

    if(strlen($cat)>0){

        $qryString .= $wpdb->prepare(" AND d.slug = %s", $cat);

    }

    $numposts = $wpdb->get_var($qryString);

    return $numposts;

}

You can prepare individual strings when necessary. You don't have to run prepare on one fully outlined query specifically.