Archived topic
Query Loop Sort by Title (ASC vs DESC)
10 replies · Started by Ian on June 21, 2022
Is there a way to reverse the sort order by Title inside Query Loop?
nvm, as usual, after spending some time without a solution, posting here always result in a solution. Found the "Order" parameter. SOVLED!
Glad you found the solution! Feel free to reach out anytime you’ll need assistance with anything else!
Along the line of ordering. When I created a Query Loop, I used "Include Posts and once published, the order was exactly as I had typed them in. In a second Query Loop, the ordering is different than the sequence they are entered. These loops are both on the same page, could this be a conflict?
There shouldn’t be a conflict.
Have you set an "order by" in the parameters? By sequence, are you referring to the sequence they were created?
Kindly let us know.
By sequence, I meant the sequence that they were inserted into the "Include Posts" parameter. For Order, what order should be set to display the sequence that they were entered?
I see. Thank you for clarifying.
You can’t order the posts through the order in which you add them in Include posts through the Query Loop block settings.
You’ll need to do it manually through a PHP snippet.
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-query' ) !== false ) {
$query_args['post__in'] = array(395672, 394831, 392713);
$query_args['orderby'] = 'post__in';
}
return $query_args;
}, 10, 2 );
Add my-query class to the Class list of the Post Template block: https://share.getcloudapp.com/geuR4AQN
Then replace 395672, 394831 and 392713 with the IDs of your Posts. Add more if needed.
If you have 2 Query loops, use something like this:
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
if ( ! empty( $attributes['className'] ) ) {
if( strpos( $attributes['className'], 'my-query-1' ) !== false ) {
$query_args['post__in'] = array(395672, 394831, 392713);
$query_args['orderby'] = 'post__in';
} else if ( strpos( $attributes['className'], 'my-query-2' ) !== false ) {
$query_args['post__in'] = array(123, 456, 789);
$query_args['orderby'] = 'post__in';
}
}
return $query_args;
}, 10, 2 );
The first Post template block will have a class of my-query-1 and the other my-query-2
Hope this helps!
Hi all,
Is there a way to reverse the query order? The oldest on top/first, latest post last/bottom?
Hi there,
if you're using a GenerateBlocks Query Loop block then add these parameters:
Orderby: Post data
Order: DESC
Thanks, David,
Found it. :)
Glad to hear that!