最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - pre_get_posts Remove tax_query Completely

programmeradmin4浏览0评论

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 );

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 );

Share Improve this question edited Sep 29, 2020 at 18:23 admcfajn asked Nov 24, 2017 at 0:40 admcfajnadmcfajn 1,3262 gold badges13 silver badges30 bronze badges 1
  • 1 Log all of the contents of the $wp_query object inside that filter, taxonomy queries are referenced in multiple places in there. I think you have to manually unset the tax query object that gets created before the filter runs, which is separate from the tax query var that set modifies. – Milo Commented Nov 24, 2017 at 1:10
Add a comment  | 

1 Answer 1

Reset to default 3

This fixed it! :) Hat-tip gmazzap for the useful info: Obliterate the main query and replace it

function wpse_286813_omit_all( $query_vars ){

  // triggered also in admin pages
  if ( is_admin() )
    return $query_vars;

  if( !empty($query_vars['taxo']) && $query_vars['taxo'] === 'all' ){
    $query_vars['taxo']='';
  }
  return $query_vars;

}
add_filter( 'request', 'wpse_286813_omit_all' );
发布评论

评论列表(0)

  1. 暂无评论