Support » Plugin: Gutenberg » Child category appears unchecked after save

  • Resolved mbrailer

    (@mbrailer)


    My blog has a parent category with several child categories. I use these for back-end organization only, so I use a filter in functions.php to hide them from readers.

    However, this filter also seems to be causing an issue with Gutenberg when saving drafts. If one or more of these child categories are checked, they become unchecked after the draft is saved.

    However, if I reload the post (say, by refreshing the browser), the selected child categories once again have their check marks.

    Here is the filter that is causing this issue. The filter only works when is_admin is false:

    // filter out admin categories
    add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
    function hide_categories_terms($terms, $post_id, $taxonomy){
    	$exclude = (array) get_term_children('353', 'category');  
    
        if (!is_admin()) {
            foreach($terms as $key => $term){
                if(in_array($term->term_id, $exclude)) unset($terms[$key]);
            }
        }
        return $terms;
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    The is_admin() checks to see if you’re in a request that is being made to the wp-admin area of the site.

    Previously, this would have been enough to distinguish such a thing. Unfortunately, this is no longer the case. The new editor gets its list of categories and such via the REST API, which works from the front end of the site, not the wp-admin section. So a REST API request to list the categories will not have is_admin set for them.

    During a REST API request, the constant of REST_REQUEST will be defined as true. So you can check if this load is a REST request like so:

    if ( defined( 'REST_REQUEST' ) && REST_REQUEST )

    However, in the long term, you might be better off checking for things like is_home and is_singlular and so forth to adjust the terms for the cases where they should be hidden, instead of inverting the logic to not hide them where they should not be hidden. This will be more forward compatible.

    Thread Starter mbrailer

    (@mbrailer)

    Thank you. It appears that checking for is_home(), is_singular(), is_archive(), or is_search() does the same thing as what !is_admin() was doing, and now the child categories remain checked during the save process.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Child category appears unchecked after save’ is closed to new replies.