I am building a nav menu programmatically like so:
$menu_exists = wp_get_nav_menu_object( 'test_menu' );
if( !$menu_exists){
$menu_id = wp_create_nav_menu('test_menu');
buildMenu($menuArray,$menu_id);
}
function buildMenu($array,$menu_id) {
foreach ($array as $menu) {
var_dump( wp_update_nav_menu_item($menu_id, 0, array(
'menu-item-title' => $menu[0]->post_title,
'menu-item-object' => 'page',
'menu-item-object-id' => $menu[0]->ID,
'menu-item-type' => 'post_type',
'menu-item-status' => 'publish',
'menu-item-parent-id' => $menu[0]->post_parent)));
if(!empty($menu[1])) {
buildMenu($menu[1],$menu_id);
}
}
}
Where $menuArray
is collection of post objects. This is working, however all the menus items get added to the top level and the menu-item-parent-id
is ignored. Does WordPress expect the menu-item-parent-id
to be the nav menu id or the post id?
$menuArray
value? At least a sample non-working$menuArray
value.menu-item-parent-id
needs a ID of an already menu item id.