Skip to content

Instantly share code, notes, and snippets.

@pbearne
Created October 29, 2013 19:38
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save pbearne/7221162 to your computer and use it in GitHub Desktop.
Save pbearne/7221162 to your computer and use it in GitHub Desktop.
how to add a tax_query to pre_get_posts in wordpress
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