WordPress.org

Support

Support » Plugins and Hacks » Category Exclusion is failing me

Category Exclusion is failing me

  • Using the example here: http://codex.wordpress.org/Custom_Queries#Category_Exclusion

    I am trying to exclude posts from a specific category from home, feed, category, archives etc – in fact I only want to see the posts from this category in admin pages (and to be able to view the post in is_single). So I include this in my functions.php file:

    add_action('pre_get_posts', 'remove_sold_cat' );
    function remove_sold_cat() {
      global $wp_query;
         if (is_admin() ) {
         } else {
         $wp_query->query_vars['cat'] = '-5';}
    }

    This code works BUT if the post is in more that one category (which they will be) the post still displays when viewing the category page of the other category. Is this a bug or a feature??

    So for category.php I now have to include this prior to the loop:
    <?php query_posts("&cat=-5"); ?>
    The posts are excluded as I expected when using the code above in functions.php BUT it screws up the paging – so I now include:
    <?php query_posts($query_string.'&cat=-5'); ?>
    The paging now works but the posts that are also in Category 5 are now included.

    My final action is to include within the loop:
    <?php if ( in_category('5') ) continue; ?>
    Yes that does remove the posts as expected but again the paging is screwed.

    I have tried this suggestion: http://wordpress.org/support/topic/57912#post-312858 but to no avail and found other reports of it not working with WP2.6.1.

    Can anyone please point me in the right direction.

Viewing 1 replies (of 1 total)
  • Thinking laterally I started to wonder if there was another way to approach this.

    In the end, instead of using a category to exclude a post I thought about using a separate author to achieve this – thus bypassing the bug / feature.

    add_action('pre_get_posts', 'remove_sold_author' );
    function remove_sold_author() {
      global $wp_query;
    if (is_admin() || is_single() ) {
    } else {
    	$wp_query->query_vars['author'] = '-4';}
    }

    This does work for me and I don’t have to mess around with any of the loops. It is a bit of a hack (I wish it would work using categories), and might have a few side effects…

Viewing 1 replies (of 1 total)
  • The topic ‘Category Exclusion is failing me’ is closed to new replies.