如何从主查询中排除词条性能最好的方式?

时间:2012-02-03 作者:kaiser

这个Q是a follow up to this answer 在Q上:"How to exclude a specific term for the search?".

筛选出具有特定术语的帖子的4种方法

Type                | Pro                   | Contra
--------------------------------------------------------
Run a new query     | Easy to implement     | Add. Queries
                                            | Add. time to execute the DB call + processing the parsing of the results
Inside the loop     | Easy to implement     | Sometimes Add. Queries
                                            | Add. time to execute
Modify the query    | Full control          | Hard to implement
                                            | Needs a lot of knowledge (preparing, DB interaction)
Add new query parts | No perform. impact    | Hard to get around
                    | Secure
                    | Full core support
第一类imho是非选项。“类型”2和;3已经在另一个Q中得到了回答,而且两个都有一些回退。

问题:如何添加其他tax_query 使用(例如)主查询的参数pre_get_posts 过滤器

1 个回复
最合适的回答,由SO网友:helenhousandi 整理而成

您可以使用pre\\u get\\u posts为主查询设置分类查询:

add_action( \'pre_get_posts\', \'my_exclude_terms_from_query\' );
function my_exclude_terms_from_query( $query ) {
    if ( $query->is_main_query() /* && whatever else */ ) {
        $tax_query = array (
                array(
                    \'taxonomy\' => \'category\',
                    \'terms\' => array( \'cat-slug\' ),
                    \'field\' => \'slug\',
                    \'operator\' => \'NOT IN\',
                )
        );
        $query->set( \'tax_query\', $tax_query );
    }
}
如果tax_query 已设置,您需要修改它,您可以抓取并添加到$tax_query 大堆

结束

相关推荐

Short code to display a loop

我一直在研究这个短代码,但没有成功。。。function loop_shortcode( $atts = \'\' ) { \'<div class=\"clear\"></div> <div class=\"childs grid_12\"> <?php $the_query = new WP_Query( array( \"post_parent\" => \