Sort Order for a Custom Query in a Post Type Archive Not Working

I have solved my problem and it had nothing to do with the query, the database or the post type.

The problem was that in trying to initially construct the query I had left a pre_get_posts function in my functions.php which was conflicting with my query. The lesson here for me is to make sure I have thoroughly checked ALL files.

The offending counter code:

// Events Pre Get Posts

function my_pre_get_posts( $query )
{
    // validate
    if( is_admin() )
    {
        return $query;
    }


    if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'events' )
    {
        $query->set('orderby', 'meta_value_num');
        $query->set('meta_key', 'event_date');
        $query->set('order', 'DESC');
    }

    // always return
    return $query;

}

add_action('pre_get_posts', 'my_pre_get_posts');

Leave a Comment