WP_QUERY不能与固定的POST_ID和TERM_TASTIONY一起使用

时间:2016-10-14 作者:Joci93

我不知道为什么,但WP\\u Query无法使用固定的post\\u id和term\\u分类法。我有一个WP\\U查询,没有固定的帖子id,什么工作正常(\'p\' => 251)

但设置post\\u id时,它不会“读取”tax\\u查询。

查询:

$args = array(
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'category\',
                \'terms\'    => \'11\',
                \'field\'    => \'term_id\'

            ),
        ),
        \'p\' => 231,
        \'post_type\' => \'events\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
        \'orderby\' => \'id\',
        \'order\'   => \'DESC\',
        \'meta_query\' => array(

            array(
                \'key\' => \'date\',
                \'value\' => $date_from,
                \'compare\' => \'>=\'

            ),

            array(
                \'key\' => \'date\',
                \'value\' => $date_till,
                \'compare\' => \'<=\'
            )

        )

    );
没有post\\u id的SQL代码:

SELECT wp_posts.* FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (12) ) AND ( ( wp_postmeta.meta_key = \'date\' AND wp_postmeta.meta_value >= \'0\' ) AND ( mt1.meta_key = \'date\' AND mt1.meta_value <= \'null\' ) ) AND wp_posts.post_type = \'events\' AND ((wp_posts.post_status = \'publish\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC 
以及post\\u id为的SQL代码:

SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1 AND wp_posts.ID = 231 AND ( ( wp_postmeta.meta_key = \'date\' AND wp_postmeta.meta_value >= \'0\' ) AND ( mt1.meta_key = \'date\' AND mt1.meta_value <= \'null\' ) ) AND wp_posts.post_type = \'events\' AND ((wp_posts.post_status = \'publish\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC
我花了几个小时来解决这个问题,但我还没有找到好的解决方案。。。

EDIT: 天哪,我找到了解决办法!

$args = array(
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'category\',
                \'terms\'    => \'11\',
                \'field\'    => \'term_id\'

            ),
        ),
        \'post__in\' => array(231), //instead of \'p\'
        \'post_type\' => \'events\',
        \'post_status\' => \'publish\',
        \'posts_per_page\' => -1,
        \'orderby\' => \'id\',
        \'order\'   => \'DESC\',
        \'meta_query\' => array(

            array(
                \'key\' => \'date\',
                \'value\' => $date_from,
                \'compare\' => \'>=\'

            ),

            array(
                \'key\' => \'date\',
                \'value\' => $date_till,
                \'compare\' => \'<=\'
            )

        )

    );

1 个回复
SO网友:Nabil Kadimi

如果您提供\'p\' 参数,然后\'tax_query\' 忽略,请参见else statment in wp-includes/query.php:L1643, 这就是\'tax_query\' 处理并添加到SQL语句中,并且只有在if (line 1634) 已跳过。