• I have 3 categories: APP NEWS / NEWS / FEATURED I wanted to display on front-end all posts with NEWS category but also if I have 1 post with 2 categories it’s display too but with NEWS category tag. So I puted on my function file:

    add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
    function hide_categories_terms($terms, $post_id, $taxonomy){
    
        // list of category slug to exclude, 
        $exclude = array('featured', 'app-news');
    
        if (!is_admin()) {
            foreach($terms as $key => $term){
                if($term->taxonomy == "category"){
                    if(in_array($term->slug, $exclude)) unset($terms[$key]);
                }
            }
        }
    
        return $terms;
    }

    but in administration when I make a change to an article and re-register it automatically deletes the assignment to the category featured or app news, except I need it anyway. Yet I have specified ‘if (!is_admin())’, I don’t understand…

    Someone have an idea please?

    Have a great day!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Category assignments are done through the REST API, so it’s not really an “admin” request per se. Only the referrer page is “admin”. Besides checking ! is_admin(), also check if REST_REQUEST constant is defined and is true.

    If your front end code also uses the REST API to get categories (it probably does not), you’d need to check the referring page URL for /wp-admin/.

    Thread Starter bassetceltia

    (@bassetceltia)

    Thank you very much for the bone track look.
    I’ve been looking all day yesterday but I haven’t been able to…

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Function ‘hide category’ removes my category in admin too’ is closed to new replies.