The found_posts
property of the WP_Query
class is what you’re looking for, which is the amount of found posts for the current query. However, you got a 0
because you set no_found_rows
(in the $query
array) to true
, which means the amount there will not be calculated. I.e.
-
WordPress will not append
SQL_CALC_FOUND_ROWS
to the SQL command for querying the posts. Otherwise, the command would be something like:SELECT SQL_CALC_FOUND_ROWS {the rest of the query}
-
WordPress will not execute
SELECT FOUND_ROWS()
after the posts query is complete. And when it’s not executed, the value of thefound_posts
property would be a zero (0
).
So remove the no_found_rows
from the $query
array, and the $results->found_posts
would have the proper value:
$query = array(
'post_type' => 'shop_coupon',
'meta_key' => 'special_key',
'fields' => 'ids',
);