How to mixi two orderby-parameters into one ordered result

I’m trying to figure out how to get an alphabetically ordered list of posts using two different orderby parameters.

Im filtering the archive query something like this:

function change_sort_order_on_archive() {
if ( is_archive() AND $query->is_main_query() ) {
$query->set( ‘meta_key’, ‘my_extra_title_field’ );
$query->set( ‘orderby’, ‘meta_value title’ );
$query->set( ‘order’, ‘ASC’ );
}
add_action( ‘pre_get_posts’, ‘change_sort_order_on_archive’);

But the results show posts sorted by meta_field first (A-Z) then shows the rest of the posts (those that don’t have the meta field) sorted by title (A-Z). IE two distinct sorted groups of posts.

What i want is one single sorted group of posts (A-Z) that uses the meta field if it exists and otherwise uses the title field to place it in order.

Is it possible or do i have to re-sort my query before printing it to the actual page?

Read more here:: How to mixi two orderby-parameters into one ordered result

Leave a Reply

Your email address will not be published. Required fields are marked *