WP Query Post Count is always 0
我的数据库中有很多预定的帖子(位置),并且我只想显示已发布帖子的分类法(位置类型)。
因此我创建了一个check_term_posts($ term_id)函数
但它总是返回0。
WP Query似乎是完成此任务的唯一方法,因为$ term-> count似乎可以提供所有内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | protected function check_term_posts($term_id) { $args = [ 'posts_per_page' => -1, 'post_type' => 'location', 'post_status' => 'publish', 'tax_query' => [[ 'taxonomy' => 'locationtype', 'field' => 'term_id', 'terms' => $term_id ]] ]; $q = new \\WP_Query($args); return $q->post_count; } |
$ q-> post_count始终为零
你必须更换
1 | 'field' => 'term_id', to 'field' => 'id', |
您的代码变成这样
1 2 3 4 5 6 7 8 9 10 | $args = [ 'posts_per_page' => -1, 'post_type' => 'location', 'post_status' => 'publish', 'tax_query' => [[ 'taxonomy' => 'locationtype', 'field' => 'id', 'terms' => $term_id ]] ]; |