Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1158800

    Hello, I’m currently using the following code to hide specific categories from displaying in the meta of my posts:

    //* Remove categories from post meta
    add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
    function hide_categories_terms($terms, $post_id, $taxonomy){
    	$exclude = array(43, 44);
    
        if (!is_admin()) {
            foreach($terms as $key => $term){
                if(in_array($term->term_id, $exclude)) unset($terms[$key]);
            }
        }
        return $terms;
    }

    But I’ve realised this is causing issues when I edit and update posts as it takes the categories off completely. Any solutions to this?

    #1159072

    Hey tomhilditch,

    Can you try to put:

    $exclude = array(43, 44);

    inside the if statement:

    if (!is_admin()) {

    Hope it helps.

    Best regards,
    Nikko

    #1159301

    Hi Nikko,

    I just tried

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

    But it’s still causing the same problem. Tried wiping cache after doing this and it still doesn’t work

    #1159905

    Hi tomhilditch,

    I have tested the code, and though upon saving it visually removes the excluded categories but if you refresh the page it doesn’t actually remove it unless you uncheck the category and save.
    I tried to look for other conditional tags that may apply but I could find none, I have tested this code with default wordpress themes and the effect is the same.

    Best regards,
    Nikko

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