【问题标题】:Wordpress Query_posts returning only 6 posts. Why?Wordpress Query_posts 仅返回 6 个帖子。为什么?
【发布时间】:2012-01-09 04:20:27
【问题描述】:

我是一名插件开发人员,我正在使用以下函数在我的插件中查询 wordpress 中的特定帖子。问题是即使$this->params['num'] 设置为 12 之类的值,查询也只返回 6 个帖子。我的插件可以找到HERE。 它对我来说很好,在大多数情况下。但是我从用户那里得到了 2 个奇怪的支持查询,在他们的情况下它不起作用。是因为旧的 wordpress 版本还是因为与他们的主题有冲突? Here 是一个冲突页面(他使用 WP 3.0.5)

private function lcp_set_categories(){
    if($this->params['name'] != '' && $this->params['id'] == '0'){
        $this->cgid = $this->get_category_id_by_name($this->params['name']);
    }else{
        $this->cgid = $this->params['id'];
    }

    $lcp_category = 'cat=' . $this->cgid;

    //Build the query for get_posts()
    $cgquery = 'cat=' . $this->cgid .
                            '&posts_per_page=' . $this->params['num'] .
                            '&orderby=' . $this->params['orderby'] .
                            '&order=' . $this->params['order'] .
                            '&exclude=' . $this->params['excludeposts'] .
                            '&tag=' . $this->params['tags'] .
                            '&offset=' . $this->params['offset'].
                            '&meta_key=' . $this->params['customfield'].
                            '&meta_value=' . $this->params['customfieldvalue'];

    $this->cgposts = get_posts($cgquery);

}

【问题讨论】:

  • 更新:修改了代码,但某些主题仍然存在问题,例如 AGGREGATE 主题。我相信它与其他一些帖子循环冲突。现在我正在使用$tmp_query = new WP_Query; 根据我的理解,我认为“查询”函数调用了不使用全局 $wp_query 对象的 get_posts 函数。所以它不应该与主题循环冲突$this->cgposts = $tmp_query->query($cgquery);
  • 你是对的。如果您正在创建一个新的 WP_Query 对象,那么它不应与您主题中的其他查询发生冲突。尝试使用 Xdebug 调试您的插件/主题以找到您的冲突。

标签: php wordpress loops


【解决方案1】:

我相信您使用错误的参数调用函数。请参阅此处http://shibashake.com/wordpress-theme/wordpress-query_posts-and-get_posts 以获得快速解释,并参阅Codex 以获得深入解释。

【讨论】:

  • 就好像你想这样做:$tmp_query = new WP_Query; $tmp_result = $tmp_query->query($cgquery);
  • 我已经修改了我的代码,见上面的评论。仍然无法使用聚合主题等某些主题。
【解决方案2】:

今天我遇到了同样的问题。经过一番搜索,我终于找到了这个解决方案:

query_posts("showposts=-1");

通过添加将show posts 设置为-1 的参数,我设法获得了所有帖子。希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2016-10-05
    • 1970-01-01
    • 2015-09-05
    • 2012-08-31
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多