How to add a child item to a menu element (using wp_nav_menu_objects)

(1) I used the code given on answer to How to use logout function on custom menu link?
to programmatically add a menu element to wp_nav_menu_objects (Say “Animals”). I give it an id, say 1000.

(2) Similarly, I succesfully created elements that I wanted to be children of a preexisting element (fixing menu_item_parent to the id of the parent element) (like “Jonquil” to element “Flowers”)

(3) The problem comes when I try to add children elements (Say “Cats” and “Dogs”) to my programmatically added element “Animals”. Cats and Dogs appear on the same level as Animals, as if WP couldn’t manage to find their parent… (Yes, I do 3 after 1…)

An idea of what I can be missing ?

Solutions Collecting From Web of "How to add a child item to a menu element (using wp_nav_menu_objects)"

you code may be look like this

    add_filter( 'wp_nav_menu_objects', 'ravs_add_menu_parent_class' );
    function ravs_add_menu_parent_class( $items ) {
      foreach ( $items as $item ) {
       print_r($item);//print each menu item an get your parent menu item-id
      }
      $link = array (
            'title'            => 'Cats',
            'menu_item_parent' => id of Animals menu like 1372,
            'ID'               => '',
            'db_id'            => '',
            'url'              => 'www.google.com'
        );
      $items[] = (object) $link;
      return $items;    
    }

Leo,

Ravs is correct to add a sub nav to an existing WP_Post_Object, but if you are creating the nav item, then want to add a subnav to that, you have to set the nav item’s db_id:

add_filter( 'wp_nav_menu_objects', 'ravs_add_menu_parent_class' );
function ravs_add_menu_parent_class( $items ) {
    $animals = array (
        'title'            => 'Animals',
        'menu_item_parent' => 0,
        'ID'               => 999999999999876, //an unlikely, high number
        'db_id'            => 999999999999876, //an unlikely, high number
        'url'              => 'www.google.com?s=animals'
    );
    $items[] = (object) $animals;

    $cats = array (
        'title'            => 'Cats',
        'menu_item_parent' => 999999999999876,
        'ID'               => '',
        'db_id'            => '',
        'url'              => 'www.google.com/?s=cats'
    );
    $items[] = (object) $cats ;

    return $items;    
}

The WordPress functions changed since the answers here in 2014.

As of today (Version 4.6.1) this code will create a main menu named “My Menu” , main item and sub item.

To run code just paste and saves in your functions.php file in your child theme.

$menu_id = wp_create_nav_menu('My Menu');

$parent_item = wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' =>  __('Main Page'),
    'menu-item-url' => home_url( '/main-page/' ), 
    'menu-item-status' => 'publish', 
    )
);

wp_update_nav_menu_item($menu_id, 0, array(
    'menu-item-title' =>  __('Sub Item Page'),
    'menu-item-url' => home_url( '/sub-item-page/' ), 
    'menu-item-status' => 'publish', 
    'menu-item-parent-id' => $parent_item)
);

Docs:

  • wp_create_nav_menu
  • wp_update_nav_menu_item