I want to filter WordPress search (and also WordPress listing of posts) on 2 values of a custom taxonomy.

I tried this code, filtering my custom taxonomy named “marque”, excluding 70 or 67 IDs (nb : in my back-office, posts can only be classified into one term of my taxonomy a time) :

// Filtering on listing
function filtre_listes( $query ) {
    if ( !is_admin() ) {
        $query->set( 'marque', '-70,-67' );
    }
    return $query;
}
add_action( 'pre_get_posts', 'filtre_listes' );

// Filtering on searches
function filtre_recherches(&$query)
{
    if ( $query->is_search && ( !is_admin() ) )
        $query->set('marque', '-70,-67');
    return $query;
}
add_action('parse_query', 'filtre_recherches');

But it doesn’t seems to work. I use WordPress 3.0.5.

Any idea ?

Thanks a lot, and sorry for my approximative english !

2 Answers
2

Try

$tax_query = array(
    'relation' => 'AND',
     array(
        'taxonomy' => 'marque',
        'terms' => array( 70, 67 ),
        'field' => 'term_id',
        'operator' => 'NOT IN'
      )
 );
 //turn it into a WP_Tax_Query object...
$tax_query = new WP_TaxQuery($tax_query);

$query->set("tax_query", $tax_query);

Leave a Reply

Your email address will not be published. Required fields are marked *