我不知道为什么,但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\' => \'<=\'
)
)
);