exclude a category from get_the_category
-
Basically, I’m using a specific/secret category to show all my posts EXCEPT one category, and I’m using this category on my index page. (Think CMS: almost all my posts are in the “artwork” category, as well as several others. Then I have a “press” category for the other posts..) I don’t want “artwork” listed when I call get_the_category from within a post; I just want its other cats listed.
I found this old thread, which offered a great/simple solution by DSGJohnDoe, but I have to do it in the core code. Basically, I add a hack in the function, like so:
function get_the_category($id = false) { global $post, $category_cache, $blog_id; $id = (int) $id; if ( !$id ) $id = (int) $post->ID; if ( !isset($category_cache[$blog_id][$id]) ) update_post_category_cache($id); $categories = $category_cache[$blog_id][$id]; //my hack unset($categories[3]); if ( !empty($categories) ) usort($categories, '_get_the_category_usort'); else $categories = array(); return $categories; }
I’d like to find a way to do this as a function or plugin – I’d prefer not to change core code. Someone else offered the following sample of how they excluded categories from a search, but I don’t know how to edit it to work as I want it for get_the_category within a post…
function myBlogPostsFilter($query) { global $wp_query; // I just want to work with searches not all global queries. if ($query->is_search) { // The line below sets/unsets the categories to search // To exclude a category just provide the cat_id with // a preceeding '-'. $query->set('cat','-3,-4,-5,-6,-7'); } return $query; } add_filter('pre_get_posts','myBlogPostsFilter');
Any help on combining the two would be greatly appreciated.Thank you!
- The topic ‘exclude a category from get_the_category’ is closed to new replies.