Sub query order not really respected...
Closed this issue · 2 comments
WillyReyno commented
Hello,
I want to display my posts with different sizes on my homepage (with a pagination)
- 2 little news
- 1 big article
- 2 little news
- 1 big article
In order to achieve this, I added a custom field "news_or_big" which equals "news" for little news, and "big" for big articles.
The problem is that at the end, the order of my queries is not respected and I get :
- 2 little news
- 2 little news
- 1 big article
- 1 big article
I don't know if I did it the right way, here's my code :
//-----------------
// Sub query #1:
//-----------------
$args1 = [
// get field news
'post_type' => 'post',
'category__not_in' => 31,
'posts_per_page' => 2,
'meta_key' => 'news_or_big',
'meta_value' => 'news'
];
//-----------------
// Sub query #2:
//-----------------
$args2 = [
// get big news
'post_type' => 'post',
'category__not_in' => 31,
'posts_per_page' => 1,
'meta_key' => 'news_or_big',
'meta_value' => 'big'
];
//-----------------
// Sub query #3:
//-----------------
$args3 = [
// get field news
'post_type' => 'post',
'category__not_in' => 31,
'posts_per_page' => 2,
'meta_key' => 'news_or_big',
'meta_value' => 'news',
'offset' => 2,
];
//-----------------
// Sub query #4:
//-----------------
$args4 = [
// get big news
'post_type' => 'post',
'category__not_in' => 31,
'posts_per_page' => 1,
'meta_key' => 'news_or_big',
'meta_value' => 'big',
'offset' => 1,
];
//---------------------------
// Combined queries #1 + #2 + #3 + #4:
//---------------------------
$args = [
'posts_per_page' => 6,
'orderby' => 'meta_value',
'paged' => $paged,
'combined_query' => [
'args' => [$args1, $args2, $args3, $args4],
'union' => 'UNION',
]
];
Thank you for your help!
therealgilles commented
Adding the following should fix your issues:
add_filter( 'cq_orderby', function( $orderby ) { return ''; });
birgire commented
Changelog
1.1.0 (2020-09-04)
Added: Ticket #19 - Add test cases for argument order workaround. (Props: therealgilles)
Cleanup: phpcs
See https://github.com/birgire/wp-combine-queries/blob/master/tests/Integration/test-combined-query.php