birgire/wp-combine-queries

query arg order

Closed this issue · 5 comments

Hello!

Thanks for the plugin :)
I'd like to order the posts from $arg1 first then $arg2, but the post is mixed. Am I doing anything wrong? Please see the codes below. (It is to show posts before "end_date" first" and then after "end date", which means expired) Thank you!


$today = date('Ymd');

$args1 = [
	'category_name' => $term_slug,
	'posts_per_page' => 100000,
	'meta_query'     => [
                [
			'key'      => 'end_date',
			 'value'    => $today,
			'compare'  => '>=',
		],
	 ],
	 'orderby' => 'modified',
	 'order' => 'DESC',

	];
				
$args2 = [
	 'category_name' => $term_slug,
	'posts_per_page' => 100000,
	'meta_query'     => [
		 [
			'key'      => 'end_date',
			'value'    => $today,
			'compare'  => '<',
		],
	 ],
	 'orderby' => 'modified',
	'order' => 'DESC',
	];

$final_query_args = [
	 'posts_per_page'      => 12,
	 'ignore_sticky_posts' => 1,
	 'combined_query' => [
		'args'   => [ $args1,$args2 ],
		'union'  => 'UNION',
	 ]
	];

$final_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$final_query = new WP_Query( $final_query_args );

Adding the following should fix your issue:

add_filter( 'cq_orderby', function( $orderby ) { return ''; });

Thanks @therealgilles

I think your suggestion could be simplified to

add_filter( 'cq_orderby', '__return_empty_string' );

You're correct. I am surprised that this is not the default behavior.

Good point, I think it was to keep the defaults of WP_Query as usual

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