1

I want to get rid of some posts, old ones, that I don't really want to keep. I was thinking to create a category named 'old' and use it as a garbage can to trash everything I don't like from old posts.

Reason I don't simply delete them is the google traffic. Some of those pages receive 1-2 visits a day, so why not leave them? just as adsense traffic :)

Anyhow.

Now I need a way to hide category 'old' from everywhere. I need a quick and dirty way to do this, preferably through functions.php - don't want to change every menu listing and add -old.

I also want to exclude posts inside this category from being displayed in 'related posts' section. But for this I guess I will have to take a look in the plugin itself...

any help?

Thanks!

1
  • I think there are other ways to hide a category without coding such as password protect it or use a category excluder plugin. I've searched around and found this article that you can reference: passwordprotectwp.com/hide-category-wordpress
    – suzie
    Sep 26, 2019 at 5:05

4 Answers 4

3

pre_get_posts is the right hook for this

since i just did category exclusion in another answer i will post it here too. Exclude the category from the WordPress loop

based on the codex sample:

http://codex.wordpress.org/Custom_Queries#Category_Exclusion

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() ) {
        $wp_query->set('category__not_in', $excluded);
    }
}
4
  • Your function will exclude posts of selected categories from the loop on the main blog listing page, from category listing pages and from archives very well, but not from Recent Posts and not from Recent Comments widgets in sidebar. How to resolve this?
    – Iurie
    Feb 25, 2014 at 19:42
  • @lurie It is probably best to ask this as a separate question. Feb 25, 2014 at 21:41
  • OK, thank you! Here is my question: wordpress.stackexchange.com/q/136017/25187.
    – Iurie
    Feb 26, 2014 at 13:57
  • 1
    Per @TheDeadMedic's answer I have revised this to work on all queries: wordpress.stackexchange.com/a/136023/6477 Feb 26, 2014 at 16:22
2

There's a plugin for that :-)

http://wordpress.org/extend/plugins/hide-categories/

2
  • 3
    I'm against using plugin for minor tasks like this. Plus you learn a thing or two looking at an actual code :)
    – user8842
    Oct 20, 2011 at 12:17
  • 1
    plugin doesn't work
    – user8842
    Mar 15, 2012 at 14:55
0

can you try this, add in your function.php

add_action('pre_get_posts', 'block_category' );
function block_category() {
global $wp_query;   
$wp_query->query_vars['cat'] = '-1';
}

replace -1 with your cat id

1
  • it didn't work :(
    – user8842
    Oct 20, 2011 at 15:46
0

can you try again with a conditional tag

function exclude_category($query) {
if ( $query->is_home ) {
    $query->set('cat', '-xx');
}
return $query;
}

xx is your category

Your Answer

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