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?
get_terms
is called, but there's aget_terms
filter on the results you could possibly use to replace it with a new fetch toget_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 callingget_terms
within, or you'll enter an endless loop.get_terms()
in this template, but they mostly have'hide_empty' => false
. Maybe this one was overlooked?