根据类别顺序,WP_QUERY会给出不同的结果

时间:2017-04-25 作者:Enrique Moreno Tent

我有个问题category__in 当我执行WP\\U查询时。

$query_args = array(
    \'post_type\'  => \'posts\',
    \'nopaging\'   => true,
);

$categories = array(1,8); // If I use this, I get 3 posts
$categories = array(8, 1); // If I use this, I get 2 posts
$query_args[\'category__in\'] = $categories;

$query = new WP_Query($query_args);
显然,我传递“array(1,8)”时得到的结果与传递“array(8,1)”时得到的结果不同。

订单重要吗???还是这是一只虫子??

UPDATE

这是本案的请求array(1,8)

string(571) "SELECT   wp_posts.* FROM wp_posts  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)  LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)  LEFT JOIN wp_term_relationships AS tt2 ON (wp_posts.ID = tt2.object_id) WHERE 1=1  AND ( 
  wp_term_relationships.term_taxonomy_id IN (1) 
  AND 
  tt1.term_taxonomy_id IN (1) 
  AND 
  tt2.term_taxonomy_id IN (1,8)
) AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC "
对于array(8,1)

string(571) "SELECT   wp_posts.* FROM wp_posts  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)  LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)  LEFT JOIN wp_term_relationships AS tt2 ON (wp_posts.ID = tt2.object_id) WHERE 1=1  AND ( 
  wp_term_relationships.term_taxonomy_id IN (8) 
  AND 
  tt1.term_taxonomy_id IN (8) 
  AND 
  tt2.term_taxonomy_id IN (1,8)
) AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC "

1 个回复
SO网友:Enrique Moreno Tent

我用下面的方法解决了

$query_args[\'tax_query\'] = array(
        \'relation\' => \'OR\',
        array(
            \'taxonomy\' => \'category\',
            \'field\'    => \'id\',
            \'terms\'    => $categories,
        ),
    );
显然,我不得不明确地说,我在过滤分类法中的“类别”。多亏了@birgire的评论,我才明白这一点。谢谢你,伙计!

Update

作为请愿书,以下是请求:

SELECT wp_posts.*
FROM wp_posts LEFT JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1=1 AND (wp_term_relationships.term_taxonomy_id IN (1,8)) 
    AND wp_posts.post_type = \'post\'
    AND (wp_posts.post_status = \'publish\'
        OR wp_posts.post_status = \'private\')
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post