Combining two wordpress queries with pagination is not working

I am trying to combine two WordPress queries but it’s not working as well as pagination.

I want to display today’s all posts sorted by comment count and after that I want to display all posts (excluding today’s post) sorted by comment count.

I believe these are the two queries to accomplish task separately. But how can I combines these so that new query lists today’s posts first sorted by comment count and then rest of the posts sorted by comment count. And also with pagination.

<?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 //

@birgire, I tried the method you suggested. But got this mysql error.

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
1

You can try the following (untested):

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 );

where we use the experimental WP_Combine_Queries class from here.

It’s currently using UNION but you might want to use UNION ALL instead.

GitHub:

The plugin is now available on GitHub here.

Leave a Comment