Count custom post types with a specific meta value

by Khom Nazid   Last Updated June 14, 2019 03:08 AM

We have a custom type called books. In a template, we need to show the counts of books that have the book_type as

  1. Fiction
  2. Non Fiction
  3. Novel
  4. Short Stories

We use ACF Pro, and the above field is set up as a checkbox multiple selection. So a book can be Fiction + Short Stories, Fiction + Novel, etc. We need to count only Fiction.

This does not work, as found in another suggested thread here:

$query = new WP_Query( array( 'meta_key' => 'book_type', 'meta_value' => 'Fiction' ) );
$fiction = $query->found_posts;

I don't have enough points to comment there, so it's better I suppose to create a new ticket.

Also found a thread on the ACF forums, but the code suggested there doesn't work either. I use latest WP, latest ACF Pro (5.8.x).

Welcome any thoughts on how to do this.

Related Questions

Check ACF fields in foreach custom taxonomy

Updated April 23, 2015 22:03 PM

Query between dates using ACF Date Picker fields

Updated April 26, 2016 08:03 AM

Can't get taxonomy terms by custom field value

Updated November 13, 2017 08:08 AM

DIVI Page Builder using Advanced Custom Fields PRO

Updated January 03, 2018 21:08 PM