Building custom API and using wp_get_nav_menu_items to get menu items. Only works when logged in, returns nothing when I’m not

Working on a custom api for a site I’m building and I found a weird issue with wp_get_nav_menu_items.

Essentially – wp_get_nav_menu_items only works and returns items when I’m logged in. If I’m not it returns a blank array.

The menu items are supposed to basically just consist of some custom post types. No pages or anything else.

I’ve tried adding the show_in_rest property when registering the post type but this does not appear to have any effect.

Is there a way around this? Or is there another way I can pull all of the items added to a menu and spit out some json?

For reference this is what the code I used to generate the json data for the menus looks like

function menu_route(){
$menu_items = wp_get_nav_menu_items(‘HCH’);

$menu = array();
foreach ($menu_items as $item ){

$menuItem = array(

“title”=>$item->title,
“dom_id”=>preg_replace(‘/[^a-z]+/i’,”_”,$item->title)
);

array_push($menu,$menuItem);
}
return $menu;
}

Thanks!

Read more here:: Building custom API and using wp_get_nav_menu_items to get menu items. Only works when logged in, returns nothing when I’m not

Leave a Reply

Your email address will not be published. Required fields are marked *