OmarQaziDev
OmarQaziDev

Reputation: 17

WordPress site giving the following error: Missing argument 2 for wpdb::prepare(). Convert?

My problem is that, after adding a new plugin to my Wordpress CMS, my site as well as the Wordpress CMS shows a PHP error:

Warning: Missing argument 2 for wpdb::prepare(), called in /home/a4921378/public_html/wp-content/themes/xscholar/framework/includes/core-functions.php on line 433 and defined in /home/a4921378/public_html/wp-includes/wp-db.php on line 1285

Image:

Error Image

Can someone please tell me what is wrong and I would be very appreciative if someone converts/fixes this piece of code.

Me: I'm an amateur coder, only understand the basics and haven't learned PHP yet, I use Wordpress quite often but something like this never came up before. I've been trying to figure out what's wrong but haven't succeeded in 2 hours.

I have already visited the pages php-warning-missing-argument-2-for-wpdb-prepare and missing-argument-2-for-wpdbprepare

$terms = $wpdb->get_results($wpdb->prepare( 'SELECT name, slug FROM ' . $wpdb->prefix . 'terms, ' . $wpdb->prefix . 'term_taxonomy WHERE ' . $wpdb->prefix . 'terms.term_id = ' . $wpdb->prefix . 'term_taxonomy.term_id AND taxonomy = "product_cat" ORDER BY name ASC;'));
$wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $attachment_url));

Upvotes: 0

Views: 238

Answers (1)

Zoli Szabó
Zoli Szabó

Reputation: 4534

You don't have to use $wpdb->prepare for your first query, because the query does not have any placeholder (What a placeholder is?). Just simply use:

$terms = $wpdb->get_results('SELECT name, slug FROM ' . $wpdb->prefix . 'terms, ' . $wpdb->prefix . 'term_taxonomy WHERE ' . $wpdb->prefix . 'terms.term_id = ' . $wpdb->prefix . 'term_taxonomy.term_id AND taxonomy = "product_cat" ORDER BY name ASC;');

Your second query has a placeholder (that %s), but it is wrapped in quotes. It should not be wrapped, the prepare method takes care of it. Just use:

$wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = %s AND wposts.post_type = 'attachment'", $attachment_url));

Upvotes: 0

Related Questions