wordpress exclude tag from get_the_tag_list

While I use custom template tag to output single post tags:

<?php
echo get_the_tag_list('<p class="text-muted"><i class="fa fa-tags"></i> ',', ','</p>');
?>

How to exclude defined tag name from the tag list?

Related posts

1 comment

  1. Well there is no filter to remove terms in get_the_tag_list but internally it calls for get_the_terms so you can add filter there.

    Consider this example:-

    add_filter('get_the_terms', 'exclude_terms');
    echo get_the_tag_list('<p class="text-muted"><i class="fa fa-tags">',', ','</i></p>');
    remove_filter('get_the_terms', 'exclude_terms');
    

    Add a filter on get_the_terms and remove it after echoing list. Because it may be called on the page multiple times.

    And in callback function remove terms by IDs or slugs

    function exclude_terms($terms) {
        $exclude_terms = array(9,11); //put term ids here to remove!
        if (!empty($terms) && is_array($terms)) {
            foreach ($terms as $key => $term) {
                if (in_array($term->term_id, $exclude_terms)) {
                    unset($terms[$key]);
                }
            }
        }
    
        return $terms;
    }
    

Comments are closed.