Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 im 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.

share|improve this question
1  
Why are you passing a WP_Query object into an argument setting method? –  t31os Dec 1 '11 at 1:06
    
Yes i was wrong, now i got it using eval to convert the string into an array (and im really sure that the string is safe). Thanks. –  José Pablo Orozco Marín Dec 1 '11 at 12:14
add comment

2 Answers

up vote 14 down vote accepted

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 );
    return $query;
}
add_filter( 'pre_get_posts', 'search_filter_get_posts' );
share|improve this answer
4  
could you give a working example of setting the tax_query inside a pre_get_posts action? –  helgatheviking Feb 22 '12 at 20:05
    
$tax_query is an object that contains a nested array. You can't override the object with a nested array. –  AlxVallejo Apr 17 '13 at 19:11
1  
$tax_query is not an object, $query is though(it's an instance of WP_Query). –  t31os Apr 17 '13 at 21:29
add comment

Tax queries require you to also set the tax_query object in the query since the query has already been parsed. See my answer for Modify Taxonomy pages to exclude items in child taxonomies.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.