0

In the navigation menu creation page, when you are trying to add a category to the menu, if the category is empty, it won't show up in the search results. However, if the category itself is empty but has a child that is not empty, it will still be shown.

I have a blog with over 500 categories, and I'm trying to add some of them to the menu but they have no posts yet. Navigating through category list is going to take time, and is also frustrating.

Now I've tracked down the issue to /wp-admin/includes/nav-menu.php, ( starting at line 588 ) but can't find a filter or hook to do so.

This line (109) seems to be responsible for doing the search:

$terms = get_terms( $matches[2], array(
    'name__like' => $query,
    'number' => 10,
));

According to the documentations, this function accepts an argument for showing empty terms 'hide_empty' => false, but I can't see such option in this part of core's code. I've added this option to the core (temporarily) to see if it solves the issue, and it does.

Can this be a bug? And is it possible to enable the search to return the result no matter the category has a post or not?

  • 1
    It doesn't appear that you can modify the args before get_terms is called, but there's a get_terms filter on the results you could possibly use to replace it with a new fetch to get_terms. All of the menu meta boxes use ajax to fetch results, and you can detect when an ajax request is happening and what action is being passed, so you can add the filter on that specific request. Just make sure to remove the filter or check args before calling get_terms within, or you'll enter an endless loop. – Milo Jul 15 '17 at 15:59
  • @Milo Thank you. I was kind of surprised when I noticed this. Why would they hide the empty categories from the results? There are 3 more get_terms() in this template, but they mostly have 'hide_empty' => false. Maybe this one was overlooked? – Jack Johansson Jul 15 '17 at 18:14
1

You can modify this behaviour by adding a custom filter to your functions.php or as a plugin:

add_filter('get_terms_args', 'wodruoso_terms_args', 10, 1); 
function wodruoso_terms_args($args) {
    /* note: I am checking here that we are in WP Admin area and that it's
    *  search by category name to minimize impact on other areas
    */
    if(is_admin() && isset($args["name__like"]) && !empty($args["name__like"])) {
        $args["hide_empty"] = false;
    }
    return $args;
}
0

The posted answer does temporarily resolve the issue, but only while doing a quick search using AJAX.

I've created and uploaded a patch on trac here. This patch fully resolves the issue, while allowing the user to exclude/include empty taxonomies from search results.

Feedbacks from anyone is appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.