admin管理员组文章数量:1435859
We have a custom type called books
. In a template, we need to show the counts of books that have the book_type
as
- Fiction
- Non Fiction
- Novel
- 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
- Fiction
- Non Fiction
- Novel
- 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 |1 Answer
Reset to default 11st. 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;
本文标签: Count custom post types with a specific meta value
版权声明:本文标题:Count custom post types with a specific meta value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745402664a2657108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$term->post_count
. – Jacob Peattie Commented Jun 14, 2019 at 6:38meta_query
is not necessary, andposts_per_page
does not need to be-1
when usingfound_posts
. – Jacob Peattie Commented Jun 14, 2019 at 12:14