Skip to content

Instantly share code, notes, and snippets.

@vishalkakadiya
Forked from pbearne/gist:7221162
Last active December 28, 2020 12:16
Show Gist options
  • Save vishalkakadiya/b011db3b8b5aec8ee3b08216f1e6775d to your computer and use it in GitHub Desktop.
Save vishalkakadiya/b011db3b8b5aec8ee3b08216f1e6775d to your computer and use it in GitHub Desktop.
how to add a tax_query to pre_get_posts in wordpress
<?php
add_action( 'pre_get_posts', array( $this,'function_pre_get_posts' ) );
function function_pre_get_posts(){
// you can't use the query->set here for tax_query
// as tax query has already been made
// so you need to need add youself to any
// existing tax query
$tax_query = array(
'taxonomy' => 'tax_name',
'field' => 'slug',
'terms' => 'term_name',
'operator'=> 'IN' );
$query->tax_query->queries[] = $tax_query;
$query->query_vars['tax_query'] = $query->tax_query->queries;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment