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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0