Wednesday, May 20, 2015

A quick way to fix the "Warning: Missing argument 2 for wpdb::prepare() " error in Wordpress

When using $wpdb->prepare(..) you may got this error:

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

Typically It is because you called the prepare function with only the query string. The correct way to do is something like this:

$wpdb->prepare( "SELECT * FROM table WHERE id = %d", $id );

But what if the function is called by a plugin written by someone and you don't want to mess with the query. Here is a quick way to fix that: Add null as the second parameter:

$wpdb->prepare( "<some query>", null );


References:

[0] https://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/
[1] https://wordpress.org/support/topic/warning-missing-argument-2-for-wpdbprepare-3