0

So I have 3 custom post types that I want to order by menu_order & have 'date' as the fallback parameter as the majority of posts will have the default menu order of '0'.

It would be nice if pre_get_posts would do this relatively simply (as opposed to something like this - Multiple orderby parameters in pre_get_posts() action), but not looking probable.

This works fine with posts where menu order is set to specific value, where it is 0, the order seems mostly random:

function order_cpt( $query ) {

if ( !is_admin() && $query->is_post_type_archive( array ( 'cat1', 'cat2', 'cat3' ) &&  query->is_main_query() ) {
$query->set( 'orderby', 'menu_order');
$query->set( 'order', 'DESC' );
return $query;
}
}
add_action( 'pre_get_posts', 'order_cpt'); 

So the first question which I can't seem to find an answer for is - how are the posts with the same menu order actually being dealt using the function above?

And is pre_get_posts the best route or should I be looking at a custom WP_Query?

Thanks

2

For identical values they'll be in whatever order they're returned by the database engine, which would typically be the order they were inserted in the database.

Anyway, the question you've linked is old, you can order by multiple columns with WP_Query:

$query->set( 'orderby', array( 'menu_order' => 'ASC', 'date' => 'ASC' ) );

pre_get_posts acts on a WP_Query instance and has the same parameters available to it, so whether pre_get_posts or a custom WP_Query is the correct choice depends on whether or not you're trying to modify the main query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.