Exclude certain tag from post meta

  • Hi,
    I’m trying to exclude the tag “incomplete,” (under “other tags” in the link below) which is an internal tag that I’d rather not be public-facing, from the dynamic data generated for a custom post type taxonomy called “other tags.”

    I used the code below, listed on this post: https://generatepress.com/forums/topic/exclude-a-specific-tag-from-the-output-of-the-tag-list/ but it didn’t seem to work; is this the correct code for what I’m trying to do?

    /* adding a function to hide the "other tag" of "incomplete" */
    
    add_filter( "term_links-post_tag", function( $term_links ) {
        $result = array();
        $exclude_tags = array( 'incomplete' );
    
        foreach ( $term_links as $link ) {
            foreach ( $exclude_tags as $tag ) {
                if ( stripos( $link, $tag ) !== false ) continue 2;
            }
            $result[] = $link;
        }
    
        return $result;
    } );

    Thanks,
    Elisabeth

  • Hi Elisabeth,

    The code you found is on the right track, but it’s using the wrong taxonomy slug — post_tag refers to WordPress’s built-in tags, not your custom taxonomy.

    Looking at your page source, your “Other tags” taxonomy slug is sign-tag, so the correct code would be:

    add_filter( 'term_links-sign-tag', function( $term_links ) {
        $result = array();
        $exclude_tags = array( 'incomplete' );
    
        foreach ( $term_links as $link ) {
            foreach ( $exclude_tags as $tag ) {
                if ( stripos( $link, $tag ) !== false ) continue 2;
            }
            $result[] = $link;
        }
    
        return $result;
    } );

    You can add this to your child theme’s functions.php or via a plugin like Code Snippets.

    Let us know if that does the trick!

  • That worked great, thank you so much!

  • Glad to know, no problem!

  • Hi,
    I’m wondering if it would be possible to add some additional terms to be excluded; I tried updating the function here:

    add_filter( 'term_links-sign-tag', function( $term_links ) {
        $result = array();
        $exclude_tags = array( 'incomplete' , 'featured-vernacular', 'featured-home', 'civic-institutional', 'commercial', 'culture-leisure', 'hospitality', 'residential-community');
    
        foreach ( $term_links as $link ) {
            foreach ( $exclude_tags as $tag ) {
                if ( stripos( $link, $tag ) !== false ) continue 2;
            }
            $result[] = $link;
        }
    
        return $result;
    } );
    

    But it didn’t work. These tags are under a different taxonomy, perhaps that’s why? They’re under the Sign Venue taxonomy (see link below). I’m trying to hide the 5 parent tags for the sign venues; the slugs are included in the code above. Thanks in advance for your help.

  • Hi Elisabeth,

    You’re right — those venue terms are under a different taxonomy, so they need their own filter hook. Here’s the updated code to replace what you currently have:

    // Exclude internal tags from "Other tags" taxonomy
    add_filter( 'term_links-sign-tag', function( $term_links ) {
        $exclude = array( 'incomplete' );
        return array_filter( $term_links, function( $link ) use ( $exclude ) {
            foreach ( $exclude as $tag ) {
                if ( stripos( $link, $tag ) !== false ) return false;
            }
            return true;
        } );
    } );
    
    // Exclude parent venue terms from "Venue" taxonomy
    add_filter( 'term_links-venue', function( $term_links ) {
        $exclude = array( 'featured-vernacular', 'featured-home', 'civic-institutional', 'commercial', 'culture-leisure', 'hospitality', 'residential-community' );
        return array_filter( $term_links, function( $link ) use ( $exclude ) {
            foreach ( $exclude as $tag ) {
                if ( stripos( $link, $tag ) !== false ) return false;
            }
            return true;
        } );
    } );

    Let us know if that sorts it!

  • Thank you George! The other tags is working great still, but with the venues because it’s hierarchical and these are the parent terms, it actually just hid all the terms in that taxonomy (so none of the signs have a venue showing up on the front end now). Each sign has a parent and a child term selected; I essentially am trying to hide the parent terms here on the front end, and only show the child terms. Does the function need to be different in this case?

  • Hi Elisabeth,

    Glad the other tags are working! The venue issue makes sense — because the taxonomy is hierarchical, the parent slug appears inside the child term URLs too (e.g. civic-institutional is in both /venue/civic-institutional/ and /venue/civic-institutional/educational/), so the filter was catching both.

    The fix is to match more precisely against the end of the URL, so only the parent links get excluded. Replace the venue filter with this:

    add_filter( 'term_links-venue', function( $term_links ) {
        $exclude = array( 'featured-vernacular', 'featured-home', 'civic-institutional', 'commercial', 'culture-leisure', 'hospitality', 'residential-community' );
        return array_filter( $term_links, function( $link ) use ( $exclude ) {
            foreach ( $exclude as $slug ) {
                if ( preg_match( '#/venue/' . preg_quote( $slug, '#' ) . '/"#', $link ) ) {
                    return false;
                }
            }
            return true;
        } );
    } );

    The term_links-sign-tag filter for “Other tags” can stay as-is.

    Let us know how it goes!

  • That seems to work – thank you so much!

  • Glad to know, no problem!

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.