SQL query and $wpdb prepare

I’ve started digging into $wpdb to query stuff from the database, I have the following query:

global $wpdb;
$sql = $wpdb->get_var(
        $wpdb->prepare(
        "
        COUNT(*)
        FROM $wpdb->posts
        WHERE post_type = 'products'
        AND post_status = 'publish'
        AND post_date >= DATE_SUB(CURDATE(), INTERVAL DAYOFMONTH(CURDATE())-1 DAY)
        "
        )
);
echo $sql;

This returns the number 3 just as wanted but also returns the following error:

wpdb::prepare was called incorrectly. The query argument of wpdb::prepare() must have a placeholder.

I know it’s probably not necessary to run prepare on this query (no user input) but I’d like to get used to it since I read it’s good practice.
So my question, how does this placeholder work? I have looked around a lot but still don’t get what it is and how it works.


Source: wpdb

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.