Skip to content

Instantly share code, notes, and snippets.

@sproutventure
Created August 5, 2011 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sproutventure/1126637 to your computer and use it in GitHub Desktop.
Save sproutventure/1126637 to your computer and use it in GitHub Desktop.
function filter_term_archive( $query ) {
if( is_tax( some_custom_post_type_tax_slug() ) ) {
$query->set( 'tax_query', array(
'relation' => 'AND', // Change to OR if we only want to filter one type.
array(
'taxonomy' => some_custom_tax_slug(),
'field' => 'slug',
'terms' => $array_of_terms
),
array(
'taxonomy' => some_custom_post_type_tax_slug(),
'field' => 'slug',
'terms' => $current_archive_term
)
));
}
return $query;
}
add_filter( 'pre_get_posts', 'my_get_posts' );
// Or this
function filter_term_archive( $query ) {
if( is_tax( some_custom_post_type_tax_slug() ) ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => some_custom_tax_slug(),
'field' => 'slug',
'terms' => $array_of_terms
)
));
}
return $query;
}
add_filter( 'pre_get_posts', 'filter_term_archive' );
@mattwiebe
Copy link

You should be running is_tax() off of the query object you're passing in, not the global $wp_query object, which the helper function is_tax() is aliased to.

function filter_term_archive( $query ) {
    if ( $query->is_tax('your_taxonomy') ) {
        // do your stuff
    }
}
add_action( 'pre_get_posts', 'filter_term_archive' );

Also, pre_get_posts is an action, not a filter. It amounts to the same thing in this case, but WP's not looking for a return value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment