Theres a way to use $query->set(‘tax_query’ in pre_get_posts filter?

The $query variable in the filter represents a WP_Query object, so you shouldn’t be passing a new WP_Query object into the method for setting that object’s properties.

The question you copied code from was incorrectly using the filter, which i feel is the crux of your issue.

Yes, tax_query can be used inside a pre_get_posts (or similarly parse_request) filter/action.

Here is an example:
Specify a custom taxonomy for search queries

function search_filter_get_posts($query) {
    if ( !$query->is_search )
        return $query;

    $taxquery = array(
        array(
            'taxonomy' => 'career_event_type',
            'field' => 'id',
            'terms' => array( 52 ),
            'operator'=> 'NOT IN'
        )
    );

    $query->set( 'tax_query', $taxquery );

}
add_action( 'pre_get_posts', 'search_filter_get_posts' );

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)