4

我正在编写一个 wordpress 循环,我想获取所有没有分配任何术语的帖子。有没有简单的方法来做到这一点?还是我真的必须获取所有术语 ID 并进行如下税务查询:

// Get all the term id's
$terms = array();
$terms = getAllTheTerms();

// Create an arguments which get all the posts that do not have a term with any
// of the id's.
$args = array(
    'post_type' => 'post',
    'tax_query' =>
        array(
            'taxonomy' => 'actor',
            'field' => 'id',
            'terms' => $terms,
            'operator' => 'NOT IN'
    )
);
$query = new WP_Query( $args );

这似乎是一个愚蠢的查询,因为就数据库而言,无需查询即可轻松获取所有帖子。

4

3 回答 3

3
$terms = get_terms( $taxonomy, array('fields'=>'ids')); /* GET ALL TERMS FROM A TOXNOMY */
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => $taxonomy,
            'field'    => 'term_id',
            'terms'    => $terms,
            'operator' => 'NOT IN' /* DO THE MAGIC -  Get all post that no in the taxonomy terms */
        )
    )
);
$the_query = new WP_Query( $args );
于 2015-11-19T14:04:48.280 回答
1

你可以试试这个

$args = array(
    'post_type' => 'post',
    'tax_query' =>
        array(
            'taxonomy' => 'actor',
            'field' => 'slug',
            'terms' => '',
    )
);
$query = new WP_Query( $args );
于 2013-10-04T20:51:52.287 回答
0

问题是:“我正在编写一个 wordpress 循环,我想获取所有没有分配任何术语的帖子。有没有简单的方法来做到这一点?”

这是答案:以下代码替换了原始海报的代码并演示了最佳解决方案。

$args = [
    'post_type' => 'post',
    'tax_query' => [
        [
            'taxonomy' => 'actor',
            'operator' => 'NOT EXISTS',
        ],
    ],
];
$query = new WP_Query($args);
于 2021-09-28T10:21:50.187 回答