Prevent pre_get_posts filter on specific post type

Improve your conditional to include a check for post type being queried. It can be done via WP_Query::get method

So, where you have

if ( !is_admin() ){
    $query->set( 'meta_key', '_ct_selectbox_52f65ae267764' );
    $query->set( 'meta_value', $city );
    return;
} 

replace with

if ( ! is_admin() && in_array ( $query->get('post_type'), array('event','venue') ) ) {
    $query->set( 'meta_key', '_ct_selectbox_52f65ae267764' );
    $query->set( 'meta_value', $city );
    return;
}

Also note that using this code, as @offroff noted in his answer, the filter will apply to all queries, the ones you triggered visiting a page (main query) and the ones being triggered via code, using new WP_Query ($args) (secondary queries).

To make the code filter not affect secondary queries, add a check for $query->is_main_query(), so the code becomes:

if ( is_admin() || ! $query->is_main_query() ) return;

if ( in_array ( $query->get('post_type'), array('event','venue') ) ) {
    $query->set( 'meta_key', '_ct_selectbox_52f65ae267764' );
    $query->set( 'meta_value', $city );
    return;
}

Leave a Comment