最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Count custom post types with a specific meta value

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jun 14, 2019 at 2:51 Khom NazidKhom Nazid 17110 bronze badges 5
  • You have to use meta query like: wordpress.stackexchange/a/159433/138292 – Nishit Manjarawala Commented Jun 14, 2019 at 5:30
  • This type of data would be much better suited to a Taxonomy. Then you can easily get the post count as it's stored alongside the taxonomy terms as $term->post_count. – Jacob Peattie Commented Jun 14, 2019 at 6:38
  • @JacobPeattie I suppose its you who downvoted my anwser ? May I ask you why ? It does the requested job, no ? – Gregory Commented Jun 14, 2019 at 12:09
  • @Gregory You make several points in your answer that are inaccurate: They shouldn't use LIKE, a full meta_query is not necessary, and posts_per_page does not need to be -1 when using found_posts. – Jacob Peattie Commented Jun 14, 2019 at 12:14
  • @JacobPeattie thanks for the reply, but i really don't think it has to be downvoted for those reason. Maybe just add a comment, & i will edit with your suggestions ! – Gregory Commented Jun 14, 2019 at 12:17
Add a comment  | 

1 Answer 1

Reset to default 1

1st. you need to add post_type to your query, then you need to filter meta_value with LIKE. Finaly, you need to add posts_per_page as -1 to get ALL posts.

$args = array(
    'post_type'=> 'books',
    'meta_query' => array(
                     array(
                        'key'     => 'book_type',
                        'value'   => 'Fiction',
                        'compare' => 'LIKE',
                    )
                ),
);
$query = new WP_Query($args);
$fiction = $query->found_posts;
发布评论

评论列表(0)

  1. 暂无评论