复杂的WP_QUERY(两个POST类型和多个运算符)

时间:2017-04-27 作者:pixedelic

我想在同一查询中显示两种不同的帖子类型。。。目前没有什么奇怪的。但我想声明这两种帖子类型的分类法包括哪些内容以及排除哪些内容,因此,例如,我想同时显示类别“16”中但不属于“19”的帖子,以及分类法“32”中不属于“34”的公文包项。

我认为这是正确的方法:

$args = array(
    \'post_status\' => \'publish\',
    \'posts_per_page\' => -1,
    \'orderby\' => \'date\',
    \'tax_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 16 ),
                \'operator\' => \'IN\'
            ),
            array(
                \'taxonomy\' => \'category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 19 ),
                \'operator\' => \'NOT IN\'
            ),
        ),
        array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'portfolio_category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 32 ),
                \'operator\' => \'IN\'
            ),
            array(
                \'taxonomy\' => \'portfolio_category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 34 ),
                \'operator\' => \'NOT IN\'
            ),
        ),
    ),
);
但它不起作用。有什么线索吗?

2 个回复
SO网友:BlueSuiter

尝试这样运行,验证结果。此外,确保有足够的帖子给你结果。

$args = array(
    \'post_status\' => \'publish\',
    \'posts_per_page\' => -1,
    \'orderby\' => \'date\',
    \'tax_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 16 ),
                \'operator\' => \'IN\'
            ),
            array(
                \'taxonomy\' => \'category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 19 ),
                \'operator\' => \'NOT IN\'
            ),
        ),
        array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'portfolio_category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 32 ),
                \'operator\' => \'IN\'
            ),
            array(
                \'taxonomy\' => \'portfolio_category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 34 ),
                \'operator\' => \'NOT IN\'
            ),
        ),
    ),
);
$wp_query = new WP_Query();
echo \'<pre>\';
print_r($wp_query);
exit;

SO网友:Tony Djukic

好的,根据法典,如果你使用tax_query 在WP\\u Query的参数中,它将更改post_type 从…起postsany, 但如果post\\u类型exclude_from_search 设置为true 那么它仍然不会包括在any. 因此,由于我们不知道portfolio post\\u类型上的配置是什么,我建议您明确指示查询搜索这两种类型posts 还有你的portfolio_post_type.

$args = array(
    //you\'ll need to put in the correct post-type for your portfolio items.
    //the codex says that if you use \'tax_query\' that post_type should default to \'any\'
    //but for the sake of it, try this...
    \'post_type\' => array( \'posts, portfolio_post_type\' ),
    \'post_status\' => \'publish\',
    \'posts_per_page\' => -1,
    \'orderby\' => \'date\',
    \'tax_query\' => array(
        \'relation\' => \'OR\',
        array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 16 ),
                \'operator\' => \'IN\'
            ),
            array(
                \'taxonomy\' => \'category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 19 ),
                \'operator\' => \'NOT IN\'
            ),
        ),
        array(
            \'relation\' => \'AND\',
            array(
                \'taxonomy\' => \'portfolio_category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 32 ),
                \'operator\' => \'IN\'
            ),
            array(
                \'taxonomy\' => \'portfolio_category\',
                \'field\'    => \'term_id\',
                \'terms\'    => array( 34 ),
                \'operator\' => \'NOT IN\'
            ),
        ),
    ),
);
以下是《法典》中讨论这一点的章节:https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters

衷心希望这能有所帮助。

相关推荐

如何控制根据Taxonomy术语显示什么模板?

我正在建立一个商业目录,并将给出一些背景,然后在最后有2个问题。The development URL is: http://svcta.lainternet.biz/The website I am rebuilding is: https://www.visitsimivalley.com/当前网站要求每个分类法类型具有唯一的业务概要文件。例如,如果您是一家酒店,并且您也有会议室和婚礼场地,那么您最终会得到3个列表,一个用于酒店,一个用于会议,一个用于婚礼。我希望有一个主配置文件,其中包含我们将显示的