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.

I have made a custom category "card_categories". I want to exclude some posts from that category on the category page. For eg :- category id 51 has posts with id's 1,2,3,4,5,6,7,8,9 Now I want that on the category page it should show posts only with post id 4,5,6,7,8,9

So, Is it possible that i can exclude posts from a category.

share|improve this question
add comment

1 Answer

If you want to exclude posts from a particular category then you could try putting this in your functions.php

function exclude_posts_from_category( $query ) {
    $catname = "Put you category name here";
    if ( $query->is_category() && ( $catname == $query->query_vars['category_name'] ) ) {
        $query->query_vars['post__not_in '] = array( 1, 2, 3 );
    }
}
add_action( 'pre_get_posts', 'exclude_posts_from_category' );

You can use $query->query_vars['post__in '] = array( 1, 2, 3 ); in case you want to specify the posts that should be included ( It will only include the post id you mention and if they are in the current category ).

share|improve this answer
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.