1

I use the bellow function (thanks to @helgatheviking!) to exclude categories from the wordpress loop. It works very well - posts of selected categories are excluded from the loop on the main blog listing page, from category listing pages and from archives, but not from Recent Posts and not from Recent Comments in sidebar. How can be extended the action of this code also on them?

add_action('pre_get_posts', 'wpa_31553' );

function wpa_31553( $wp_query ) {

    //$wp_query is passed by reference.  we don't need to return anything. whatever changes made inside this function will automatically effect the global variable

    $excluded = array(272);  //made it an array in case you need to exclude more than one

    // only exclude on the front end
    if( !is_admin() ) {
        set_query_var('category__not_in', $excluded);
        //which is merely the more elegant way to write:
        //$wp_query->set('category__not_in', $excluded);
    }
}

UPDATE: A small clarification, the excluded categories have not disappeared also from the Categories widget. Just disappeared all posts from these categories when I open them with a mouse click. I would like that they disappear also from the Categories widget.

  • Are you referring to the recent posts widget? – Brad Dalton Feb 26 '14 at 14:08
  • Yes, I mean the Recent Posts and the Recent Comments widgets. – Iurie Malai Feb 26 '14 at 18:37
  • Excluding from widgets and home page loop are different and really should be 2 questions. Also you didn't provide any code of your own. – Brad Dalton Feb 27 '14 at 2:46
3

The original author isn't quite right in saying "which is merely the more elegant way to write".

set_query_var() will always override the main query, whereas if you actually use:

$wp_query->set( 'category__not_in', $excluded );

... it will work for any instance of query_posts(), such as the recent posts widget.

2

Per @TheDeadMedic, I have adjusted my code. Hopefully it will now work on all non-admin queries.

add_action('pre_get_posts', 'wpa_136017' );

function wpa_136017( $wp_query ) {

    //$wp_query is passed by reference.  we don't need to return anything. whatever changes made inside this function will automatically effect the global variable

    $excluded = array(272);  //made it an array in case you need to exclude more than one

    // only exclude on the front end
    if( !is_admin() ) {
        $wp_query->set('category__not_in', $excluded);
    }
}
  • @TheDeadMedic - OK, a half of the problem was solved, excluded categories not shown more in the Recent Posts widget. Partially they disappeared also from Recent Comments widget, but only those without comments, the posts with comments was not excluded :(. – Iurie Malai Feb 26 '14 at 18:59
  • @TheDeadMedic - A small clarification, the excluded categories have not disappeared also from the Categories widget. Just disappeared all posts from these categories when I open them with a mouse click. It would have been nice to disappear also from the Categories widget. -- I updated my question. – Iurie Malai Feb 26 '14 at 19:13
  • Well they wouldn't disappear from the get_categories() or get_terms() function that is creating the Categories widget. That is a totally different function and doesn't pass through the pre_get_posts filter. – helgatheviking Feb 26 '14 at 21:19
1

This is what i would use to exclude categories from the categories widget

function widget_categories_args_filter( $cat_args ) {

$cat_args['exclude'] = array(1,2,3);

return $cat_args;
}

add_filter( 'widget_categories_args', 'widget_categories_args_filter', 10, 1 );

There are NO filters to exclude categories from the recent posts or recent comments widgets. You could rebuild the widget using this solution as a guide http://wordpress.org/support/topic/recent-posts-widget-with-category-exclude

  • Sorry, I don't have my own code, but what I need should happen in a single action. – Iurie Malai Feb 27 '14 at 6:27
  • Thank you for your code! I will try to combine it with the @helgatheviking function. – Iurie Malai Feb 27 '14 at 6:55
0

I excluded my selected categories from everywhere with the Advanced Category Excluder (ACE) plugin, except from Categories widget - here helped the @Brad Dalton code. ACE has an own Recent Comments widget that hides excluded categories, but here was a minor problem on 404 page, where this widget continues to show excluded categories that have comments, so I redirected it (the 404 page) to the home page of my site with the 404 to Start plugin.

Thank you all for your help!

UPDATE

The Advanced Category Excluder plugin doesn't have an option to select for which user roles are excluded specific categories, so I disabled it and also disabled the Recent Comments widget, for which I cannot exclude, for now, the needed categories. In conclusion, the only solution was the @helgatheviking and @Brad Dalton functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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