WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Would like to filter my home page posts by category OR by tag.

i.e. only posts that are in category "X" or have tag "B" appear on the home page.

Have used code below successfully to filter (include/exclude) just by category

function exclude_category($query) {
if ( $query->is_home ) {
$query->set('cat', '3');
return $query;
add_filter('pre_get_posts', 'exclude_category');

then tried to add the tag filter by replacing $query->set('cat', '3'); with $query->set(('cat', '3')||(is_tag('urgent'));

no good - did not work.

Can this be done ? How ?

Any ideas, help or advice much appreciated

share|improve this question

You can use the following code to list the posts that are categorized under category id 3 OR have tag 'urgent'

function exclude_category($query) {
    if ( $query->is_home ) {
        $query->set( 'tax_query', array(
            'relation' => 'OR',
                'taxonomy' => 'category',
                'field' =>  'ID',
                'terms' => 3
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => 'urgent'
    return $query;
add_filter('pre_get_posts', 'exclude_category');
share|improve this answer
Excellent and thanks! – Valentine Cosgrave Jun 20 '11 at 8:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.