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

Theres a way to use $query->set('tax_query', ...) in pre_get_posts filter? for example next code is not altering the query. Note that I’m building $taxonomies from and custom search.

function custom_search_filter($query) {
        ...

        // array('taxonomy' => 'category', 'field' => 'id', 'terms' => array( 41,42 ), 'operator' => 'IN')
        $taxonomies = implode(',', $taxonomy_arr);

        // https://wordpress.stackexchange.com/questions/25076/how-to-filter-wordpress-search-excluding-post-in-some-custom-taxonomies

        $taxonomy_query = array('relation' => 'AND', $taxonomies);

        $query->set('tax_query', $taxonomy_query);
    }

    return $query; 
}


add_filter( 'pre_get_posts', 'custom_search_filter', 999 );

Thanks in advance.

Related posts

Leave a Reply

2 comments

  1. 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 action, which may have been the crux of your issue.

    Yes, tax_query can be used inside a pre_get_posts action or similarly by hooking parse_request, request or parse_query.

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

    function search_filter_get_posts( $query ) {
        
        if( !$query->is_search || is_admin() )
            return;
        
        $taxquery = array(
            array(
                'taxonomy' => 'career_event_type',
                'field' => 'term_id',
                'terms' => array( 35, 19, 6 ),
                'operator'=> 'IN'
            )
        );
        $query->set( 'tax_query', $taxquery );
        
    }
    add_action( 'pre_get_posts', 'search_filter_get_posts' );
    

    Although it should be noted more specificity with your conditional parameters might help in avoiding making alterations to queries you do not intend to.

    For example, if you are utilising a custom search form to search on a custom post type, including a hidden field(or even a select box) to set the post_type would help out with providing extra conditional specificity.

    <input type="hidden" name="post_type" id="post_type" value="my_post_type" />
    

    The above example pre_get_posts (or equivalent) action/filter would then have more information to validate we’re modifying the right kind of request/query.

    'my_post_type' === $query->query['post_type']
    

    or

    'my_post_type' === get_query_var( 'my_post_type' )
    

    You could go further and use a query var parameter you’re not otherwise expecting in the search query that WordPress supports, just to act a flag for your action/filter.

    <input type="hidden" name="author_name" id="author_name" value="my_special_guy" />
    

    This provides another conditional parameter for your action to look for, and you can simply unset the value before it reaches the query.

    set_query_var( 'author_name', '' );