Custom WP_Query breaks default behaviour of viewing right post associated with tax-term!

You second query performs an entirely new query and does not have the terms set.

Besides, it’s not as efficient to ‘redo’ the query. Instead, hook into pre_get_posts and change the order there:

function change_order_for_events( $query ) {
    //Check if currenty query is the 'main query' and event type taxonomy being viewed 
    if ( $query->is_main_query() && is_tax('event_type')) {
        $query->set( 'meta_key', '_wr_event_date' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'change_order_for_events' );

For bonus points you might want to check that the ‘orderby’ and ‘order’ paramters are not set – that way ordering by date becomes default for events on the event type taxonomy pages but can be overridden if required.

All conditionals are available to you.

Leave a Comment