In a taxonomy archive page I want change the order my different custom post appear.

Basically I am looking to achieve this order:

  • Future events ordered by “date_of_event” custom field in DESC
  • All my other custom posts ordered randomly
  • Old events ordered by “date_of_event” custom field in ASC.

I have added a pre_get_posts filter in my function page which looks like this:

function fwp_archive_per_page( $query ) {
    if ( is_tax() ) {

        $query->set('meta_query', 
            array(
                'relation' => 'OR',
                array(
                    'key' => 'date_of_event',
                    'compare' => 'NOT EXISTS',
                ),
                array(
                    'key' => 'date_of_event',
                    'value' => date("Ymd"),
                    'compare' => '>',
                ),
                array(
                    'key' => 'date_of_event',
                    'value' => date("Ymd"),
                    'compare' => '<',
                ),
            )
        );
        $query->set('orderby', 
            array(
                'meta_value_num' => 'DESC',
                'title' => 'DESC',
            )
        );
        //$query->set('posts_per_page', -1);
    }
}
add_filter( 'pre_get_posts', 'fwp_archive_per_page' );

This gives me this order.

  • Future events ordered by “date_of_event” custom field (sooner events first)
  • Old events ordered by “date_of_event” custom field (closer events first)
  • All my other custom posts ordered by title

Is it possible to achieve this by filtering the existing archive query?

Thank you.

Read more here: Different orders for different content within a wordpress Query


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: