WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to exclude a category and its subcategories in posts. This is the code I'm working on and it works:

        $categories = wp_get_post_terms($post->ID, 'category');
        $categories = wp_list_filter($categories, array('slug'=>'regione'), 'NOT');
        $categories = wp_list_filter($categories, array('slug'=>'lazio'), 'NOT');
        $categories = wp_list_filter($categories, array('slug'=>'rm'), 'NOT');
        if (!empty($categories)) {
            <ul class="category">
                foreach ($categories as $category) {
                    $cat = get_category($category);
                    echo '<li><a href="' . get_category_link($category) . '">' . $cat->name . '</a></li>';

Although, the category 'regione' has more than 20 subcats and each subcat has 5 to 10 other subcats: any suggestion on how to exclude in a better way the main category and its children?

share|improve this question

I am answering a question after like 6 months, so I expect the quality of my answer not upto WPSE standards and hence would love feedback from other experience WPSE gurus.

Add the following function to your functions.php

function filter_category_list_by_slug( $slug, $categories ) {
    $excluded_parent = get_category_by_slug( $slug );
    $excluded_cats = get_categories( array( 'child_of' => $excluded_parent->term_id ) );
    $excluded_cats[] = $excluded_parent;
    $filtered = array();
    $matched = false;
    foreach ($categories as $category ) {
        $matched = false;
        foreach( $excluded_cats as $ex_cat ) {
            if ( $category->term_id == $ex_cat->term_id ) {
                $matched = true;
        if ( ! $matched ) {
            $filtered[] = $category;
    return $filtered;

And the filter the category list like following:

$categories = wp_get_post_terms($post->ID, 'category');
$categories = filter_category_list_by_slug( 'regione', $categories );
$categories = filter_category_list_by_slug( 'lazio', $categories );
$categories = filter_category_list_by_slug( 'rm', $categories );
share|improve this answer
Ciao Hameedullah, I tried out your code but the post doesn't output any category. Could you check it out again? Grazie, diana – diana Jun 27 '12 at 16:25
I worked it out! Had to change 'parent1' to my cat named 'regione' in the functions.php file and all is perfect! Thank you so much, very good indeed! Grazie again, diana – diana Jun 27 '12 at 16:33
sorry diana it was a typo, I fixed the code. it was using the hardcoded parent1 category instead of the one being passed to the funciton. – Hameedullah Khan Jun 28 '12 at 12:05

Your Answer


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.