最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Admin: WooCommerce Navigation Menus do not show empty categories search tab

matteradmin8PV0评论

I have been searching and finally found a question that answers my question:

How to show empty category in admin menus search

How could this be implemented as a quick fix? I am trying to build a complex WooCommerce menu but the search tab does not bring up empty categories which has become a pain.

I have been searching and finally found a question that answers my question:

How to show empty category in admin menus search

https://core.trac.wordpress/ticket/45298

How could this be implemented as a quick fix? I am trying to build a complex WooCommerce menu but the search tab does not bring up empty categories which has become a pain.

Share Improve this question asked Feb 1, 2019 at 10:23 RobRob 193 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

When you perform a search for terms in the menu editor, it runs get_terms() with the name__like argument (a category is a type of "term").

The get_terms_args filter lets you filter any use of get_terms(). So we can use this filter and check if the name__like argument has a value. If it does then that tells us this is a search for a term, in which case we will force the query to include empty terms:

function wpse_327345_search_empty_terms( $args, $taxonomies ) {
    if ( ! empty( $args['name__like'] ) ) {
        $args['hide_empty'] = false;
    }

    return $args;
}
add_filter( 'get_terms_args', 'wpse_327345_search_empty_terms', 10, 2 );

Note however that this will also affect searches for terms in other areas of the admin, like the post edit screen and categories list. If you only want to include empty terms when searching from the menu editor, you can check if the query is part of the AJAX request that performs the search by checking if $_POST['action'] is exists and equals menu-quick-search:

function wpse_327345_search_empty_terms( $args, $taxonomies ) {
    if ( isset( $_POST['action'] ) && $_POST['action'] === 'menu-quick-search' ) {
        if ( ! empty( $args['name__like'] ) ) {
            $args['hide_empty'] = false;
        }
    }

    return $args;
}
add_filter( 'get_terms_args', 'wpse_327345_search_empty_terms', 10, 2 );
Post a comment

comment list (0)

  1. No comments so far