【问题标题】:WP Query Post Count is always 0WP Query Post Count 始终为 0
【发布时间】:2019-05-16 08:40:09
【问题描述】:

我的数据库中有很多预定的帖子(位置),我只想显示已发布帖子的分类法(位置类型)。

因此我创建了一个 check_term_posts($term_id) 函数

但它总是返回 0。

WP Query 似乎是实现这一目标的唯一方法,因为 $term->count 似乎可以提供一切。

    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 始终为零

【问题讨论】:

标签: wordpress api


【解决方案1】:

你必须更换

'field' => 'term_id', to 'field' => 'id',

你的代码变成这样

$args = [
                'posts_per_page' => -1,
                'post_type' => 'location',
                'post_status' => 'publish',
                'tax_query' => [[
                    'taxonomy' => 'locationtype',
                    'field' => 'id',
                    'terms' => $term_id
                ]]
            ];

【讨论】:

    猜你喜欢
    • 2012-04-13
    • 1970-01-01
    • 2019-02-28
    • 2023-03-10
    • 2014-01-09
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多