Warning: Missing argument 2 for wpdb::prepare() Fixed

When I used a footer that collects the earliest to the newest year to display from the dated posts, I got the “Warning: Missing argument 2 for wpdb::prepare()” on the bottom of the footer. After Googling the problem, I found the fix for it. It’s not a good way to mask the problem like I did before.

wpdb_prepare_1     wpdb_prepare_2

The bad codes:

wpdb_prepare_3

 $post_datetimes = $wpdb->get_row($wpdb->prepare(“SELECT YEAR(min(post_date_gmt)) AS firstyear, YEAR(max(post_date_gmt)) AS lastyear FROM $wpdb->posts WHERE post_date_gmt > 1970”));

The good codes: We need to add the blank second argument to make it complete.”1970″,’ ‘));”

wpdb_prepare_fixed

$post_datetimes = $wpdb->get_row($wpdb->prepare(“SELECT YEAR(min(post_date_gmt)) AS firstyear, YEAR(max(post_date_gmt)) AS lastyear FROM $wpdb->posts WHERE post_date_gmt > 1970”,’ ‘));

Leave a Reply

Your email address will not be published. Required fields are marked *