pre_get_posts Remove tax_query Completely

Question

I’m using pre_get_posts to filter by using $wp_query->set( 'tax_query', $tax_query );

I’m probably better-off doing this with a new WP_Query, but if someone knows something I don’t I’d love to hear it

Problem, I need to set a default taxonomy & preserve the ability to show all posts.

For instance:

/cpt/?taxo=this shows all cpt’s which have a taxo-term == this.

$taxo_terms = [];
if( !empty( $_GET['taxo'] ) ){
  $types = explode(',', $_GET['taxo']);
  foreach ($types as $key => $val) {
    $taxo_terms[$key] = sanitize_key($val);
  }
}
else{
    $taxo_terms[]='this'; // here's where things get ugly.
}

$tax_query = [];
if( !empty($taxo_terms) ){
  $tax_query[] = [
    'taxonomy' => 'taxo',
    'field' => 'slug',
    'terms' => $taxo_terms,
    'operator' => 'AND',
  ];
  $wp_query->set( 'tax_query', $tax_query );
}

I want to add /cpt/?taxo=all to show all posts, regardless of what taxo->terms they have.

Hoping to find a way to unset the current tax_query from the loop, using pre_get_posts.

Is there a way to $wp_query->UNSET?

I’ve tried $wp_query->set( 'tax_query', null );

0
admcfajn 4 months 0 Answers 17 views 0

Leave an answer