WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 4 months ago

#28642 new defect (bug)

wp_get_nav_menu_items retrieving empty array

Reported by: furi3r Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 3.9
Component: Menus Keywords: close
Focuses: Cc:

Description

Hi Guys, I found out this problem and it is a bit weird, I tried to debug, but it looks everything on my side is correct, and is just the function failing.

Environment:
WP_DEBUG on
PHP logs clear
WP latest

Situation:
I use a WP menu on the header. Every template calls to the_header() and inside header.php I call to :
wp_nav_menu(array(

'theme_location' => 'main',
'container' => 'nav',
'menu' => 'main' ));

It works like a charm everywhere but in a specific archive of a custom taxonomy (it works fine for others custom taxonomies).
Trying to debug I empty the full template and leave only the_header() the_footer() to avoid conflicts, still happens the same.

I started to debug using other functions related:

In every archive I get the proper array with the menu in the last call, but in the one failing i get:

var_dump(get_nav_menu_locations());
array(5) { primary?=> int(122) bottom?=> int(124) secondary?=> int(0) mobile-menu?=> int(122) lang?=> int(125) }

var_dump(is_nav_menu(122));
bool(true)

var_dump(wp_get_nav_menu_items('main'));
array(0) { }

Any ideas?

Change History (6)

#1 @furi3r
3 years ago

  • Version changed from 3.9.1 to 3.9

Sorry my WP version is 3.9

This ticket was mentioned in IRC in #wordpress-dev by furi3r. View the logs.


3 years ago

#3 @furi3r
3 years ago

Update:

This is happening in more archives when im performing a search in it.

This archive /xxxx/ is an archive of a taxonomy.
I've just realised the menus are working when loading /xxxx/

But if i perform a search query /xxxx/?search=xxx is when the menus are not loading.

Could be some interference between how this menu works and the fact im using a custom search query?

#4 @chriscct7
2 years ago

  • Keywords close added

Can't replicate

#5 follow-up: @jesseisrad
4 months ago

I CAN reproduce this.

I'm trying to work with this function in the footer template and it works fine on a post page, but not on any taxonomic archive. Returns an empty array on those pages.

I'm on WP 4.8

Last edited 4 months ago by jesseisrad (previous) (diff)

#6 in reply to: ↑ 5 @jesseisrad
4 months ago

apologies, my issue and maybe that of the op is related to this: https://wordpress.org/support/topic/wp-nav-menu-dissapears-in-category-pages-1/

basically there's a pre_get_posts somewhere that needs to have 'nav_menu_item' post-type added to it.

Note: See TracTickets for help on using tickets.