获取所有与WP_Query无关的帖子

时间:2013-10-01 09:20:37

标签: wordpress

我正在写一个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 );

这看起来像是一个愚蠢的查询,因为数据库方面很容易在没有查询的情况下获取所有帖子。

2 个答案:

答案 0 :(得分:2)

$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 );

答案 1 :(得分:1)

你可以试试这个

$args = array(
    'post_type' => 'post',
    'tax_query' =>
        array(
            'taxonomy' => 'actor',
            'field' => 'slug',
            'terms' => '',
    )
);
$query = new WP_Query( $args );