How do I count only the post that have a specific custom meta field value?
$productCount = wp_count_posts('product');
echo $productCount->publish;
That gives me all the total of all the post.
How do I find out how many of those post have say a custom meta value of cat
?
2 Answers
There is no default/native function in wordpress to count posts from a specific meta value. wp_count_posts
only counts the amount of posts according to post type
It might be useful here to use WP_Query
to create a custom query to get all posts with that specific meta value and then use $wp_query->found_posts
to get the post count.
This should do it
$query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'blue' ) );
echo $query->found_posts;