将两个WordPress查询与分页组合在一起不起作用

时间:2014-08-26 作者:Robert hue

我正在尝试合并两个WordPress查询,但效果不如分页。

我想显示今天按评论数排序的所有帖子,然后我想显示按评论数排序的所有帖子(不包括今天的帖子)。

我相信这是分别完成任务的两个查询。但我如何组合这些内容,以便新的查询首先按评论数排序列出今天的帖子,然后按评论数排序列出其余帖子。还有分页。

<?php

  $today = getdate();

  $args1 = array(
    \'post_type\' => \'post\',
    \'orderby\' => \'comment_count\',
    \'ignore_sticky_posts\' => 1,
    \'date_query\' => array(
      array(
        \'year\'  => $today[\'year\'],
        \'month\' => $today[\'mon\'],
        \'day\'   => $today[\'mday\'],
      ),
    ),
    \'paged\' => $paged,
  );

  $query1 = new WP_Query( $args1 );

  $args2 = array(
    \'post_type\' => \'post\',
    \'orderby\' => \'comment_count\',
    \'ignore_sticky_posts\' => 1,
    \'paged\' => $paged,
  );

  $query2 = new WP_Query( $args2 );

?>
EDIT: 1 //

@伯吉尔,我试过你建议的方法。但出现了mysql错误。

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'-5,5\' at line 1]
SELECT SQL_CALC_FOUND_ROWS * FROM ( (SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND ( ( post_date > \'2014-08-27 00:00:00\' ) ) AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') ORDER BY wp_posts.comment_count DESC LIMIT 1000) UNION ALL (SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND ( ( post_date < \'2014-08-27 00:00:00\' ) ) AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') ORDER BY wp_posts.comment_count DESC LIMIT 1000 ) ) as combined LIMIT -5,5

1 个回复
最合适的回答,由SO网友:birgire 整理而成

您可以尝试以下操作(未经测试):

Setup the query arguments #1: (today)

  //-----------------
  // Query part #1:
  //-----------------
  $args1 = array(
     \'post_type\'           => \'post\',
     \'orderby\'             => \'comment_count\',
     \'ignore_sticky_posts\' => 1,
     \'date_query\'          => array(
          array(
              \'after\' => date(\'Y-m-d\'),
          ),
         \'inclusive\'  => true,
      )
  );

Setup the query arguments #2: (!today)

  //-----------------
  // Query part #2:
  //-----------------
  $args2 = array(
      \'post_type\'           => \'post\',
      \'orderby\'             => \'comment_count\',
      \'ignore_sticky_posts\' => 1,
      \'date_query\'          => array(
          array(
              \'before\' => date(\'Y-m-d\'),
          ),
         \'inclusive\'  => false,
      )
  );

Then we combine it:

//---------------------------
// Combined queries #1 + #2:
//---------------------------
$args = array( 
   \'posts_per_page\' => 5,
   \'paged\'          => ( $paged = get_query_var( \'paged\' ) ) ? $paged : 1 ,
   \'sublimit\'       => 1000,
   \'args\'           => array( $args1, $args2 ),
);
$results = new WP_Combine_Queries( $args );
我们在哪里使用实验WP_Combine_Queriesfrom here.

它当前正在使用UNION 但你可能想使用UNION ALL 相反

GitHub:

该插件现在在GitHub上可用here.

结束

相关推荐

Custom template pagination

我有一个自定义的帖子类型“新闻”。我创建了id为55的“News”页面和一个自定义模板文件。我想实现分页。问题是,当我访问设置了页码的新闻时,会出现错误“404未找到”。URL类似于“/新闻/页面/2/”。我使用paginate_links 函数以显示分页。如何显示正确的页面?