Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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',
            array(
                'taxonomy' => 'category',
                'field' =>  'ID',
                'terms' => 3
            ),
            array(
                '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
add comment

Your Answer

 
discard

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.