WP_Query tax_query problem

时间:2014-04-07 作者:user3256672

当我在中调用WP\\u Query with tax Query或category\\uu时,我只从请求的类别数组中的第一个类别获取帖子。

$query = new WP_Query(
    array(
        "tax_query" => array(
            array(
                "taxonomy" => "category",
                "field" => "term_id",
                "terms" => array(1, 2, 3)
            )
        ),
        "posts_per_page" => -1
    )
);
下面是来自$query->request的sql查询:

SELECT wp_posts.*
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
INNER 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,
                                                  2,
                                                  3)
       AND tt1.term_taxonomy_id IN (1)
       AND tt2.term_taxonomy_id IN (1))
  AND wp_posts.post_type = \'post\'
  AND (wp_posts.post_status = \'publish\'
       OR wp_posts.post_status = \'future\'
       OR wp_posts.post_status = \'draft\'
       OR wp_posts.post_status = \'pending\'
       OR wp_posts.post_status = \'private\')
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
结果仅列出第1类中的职位。我的问题是什么?

1 个回复
SO网友:user3256672

发现问题。我替换了:

$posts = $query->get_posts();
使用:

$posts = array();
if ($query->have_posts()){
  while ($query->have_posts()){
    $query->next_post();
    array_push($posts, $query->post);
  }
}
现在一切都正常了。

结束

相关推荐

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

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