Making a query to the DB using same parameters of loop

So, I have a loop that works great.

But I need to count how many posts that loop has, and I need to do it via a query to the DB.

My loop looks like:

$adsArg = array(
‘offset’ => $offset,
‘post_type’ => ‘ads’,
‘meta_key’ => $metaKey,
‘tax_query’ => $taxQuery,
‘meta_query’ => $metaValue,
);
$adsQuery = new WP_Query($adsArg);

And the query I have so far looks like:

$wpdb->get_var($wpdb->prepare(“SELECT COUNT(ID) FROM ” . $table_name . ” WHERE post_type = ‘ads’ AND post_status = ‘publish’ AND tax_query = ” . $taxQuery . ” AND meta_query = ” . $metaValue . “, array() ) );

It’s not working and I have no idea the proper way to do that.

The counter has to be done this way, with a direct query to the database.

Read more here:: Making a query to the DB using same parameters of loop

Leave a Reply

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