[Support request] hiding specific categories from meta

Home Forums Support [Support request] hiding specific categories from meta

Home Forums Support hiding specific categories from meta

  • This topic has 9 replies, 2 voices, and was last updated 5 months ago by Elvin.
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #547523
    Dale

    Hi! I have a couple categories that I only use for organization on the back end and don’t want visible on the front end—meaning I’d like to hide them from the meta area that appears below each post. I tried this, where “feature” and “10” are standins for the category:

    function the_category_filter($thelist,$separator=’ ‘) {
    if(!defined(‘WP_ADMIN’)) {
    //list the category names to exclude
    $exclude = array(‘feature’);
    $cats = explode($separator,$thelist);
    $newlist = array();
    foreach($cats as $cat) {
    $catname = trim(strip_tags($cat));
    if(!in_array($catname,$exclude))
    $newlist[] = $cat;
    }
    return implode($separator,$newlist);
    } else
    return $thelist;
    }
    add_filter(‘the_category’,’the_category_filter’,10);`

    pasted into Code Snippets and activated, but it didn’t work. Thoughts?

    GeneratePress 2.0.2
    GP Premium 1.6.2
    #547949
    Tom
    Lead Developer
    Lead Developer
    #548534
    Dale

    Thanks for your response.

    That plug-in hides actual posts. I am just looking to hide the name of the category from the meta that appears beneath each post.

    For example, on page https://www.dalecameronlowry.com/submission-call-fairy-tales-about-summer-nights-pays-20-due-may-20/, I have two categories showing in the meta at the bottom of the page: “Calls for Submissions, For Writers”

    let’s say that I don’t want readers clicking on “For writers” because it’s only used for backend organization. So I want to keep the post in that category, but have the meta only read “Calls for Submissions”

    Is that clearer?

    #548939
    Tom
    Lead Developer
    Lead Developer

    Ah, gotcha.

    Untested, but you could try this filter:

    add_filter( 'get_terms', 'tu_remove_terms' );
    function tu_remove_terms( $terms ) {
        unset( $terms['category_slug'] );
    
        return $terms;
    }
    #549322
    Dale

    Thanks. I added it like this to Code Snippets:

    add_filter( 'get_terms', 'tu_remove_terms' );
    function tu_remove_terms( $terms ) {
        unset( $terms ('for-writers') );
    
        return $terms;
    }

    But nothing happened. I looked in incognito and purged the site-side caches as well. Did I adapt it incorrectly?

    #549669
    Tom
    Lead Developer
    Lead Developer

    Hmm, should have worked.

    Let’s try this function: https://codex.wordpress.org/Function_Reference/wp_remove_object_terms

    wp_remove_object_terms( get_the_ID(), 'for-writers', 'category' );

    #549815
    Dale

    That doesn’t seem to work either. I’ll keep looking for solutions.

    #549980
    Tom
    Lead Developer
    Lead Developer

    Hmm, strange. It may be worth asking on stackoverflow.com. Someone may have done this before.

    You could use tags instead of categories for the internal tags. Then just choose not to display tags on the frontend.

    #1742920
    Lorenzo

    I neded the same and It works in my case with this code:

    add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
    function hide_categories_terms($terms, $post_id, $taxonomy){
    	$exclude = array(IDcat1,IDcat2...);
    
        if (!is_admin()) {
            foreach($terms as $key => $term){
                if(in_array($term->term_id, $exclude)) unset($terms[$key]);
            }
        }
        return $terms;
    }

    Source

    #1743820
    Elvin
    Staff
    Customer Support

    Hi Lorenzo,

    Thank you for sharing it with us. 🙂

    A wise man once said:
    "Have you cleared your cache?"

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