4

I'm using the code below to create custom menu items on the fly. Its working great, except all menus are created as "Custom". In the code below, setting the menu-item-type to 'page' appears to have no effect.

        foreach($thePages as $page){
            wp_update_nav_menu_item($menu->term_id, 0, array(
                'menu-item-title' => $page->post_title,
                'menu-item-type' => 'page', 
                'menu-item-status' => 'publish')
            );          
        }

wp-includes/nav-menu.php shows that the value is hard coded to "Custom". However, if I manually add a page to the menu, using the WP Menus manager, it lists it as "Page", not "Custom". What can I do to set the menu type to "Page"?

5
  • I've used the filter 'nav_menu_css_class' to modify the css classes. Is this what you wanted?
    – Cristian
    Commented Apr 21, 2011 at 19:36
  • @Wild Phoenix: I'm creating a custom menu on the fly, then iterating over the top level pages and assigning each of them to the custom menu. This is all done via a plugin during its activation hook. Its not related to css at all at this point. I'm just trying to figure out how to get each menu registered as a "Page" and not as "Custom" in my code above.
    – Scott B
    Commented Apr 21, 2011 at 19:42
  • try setting menu-item-object-id in your array to the page id
    – Bainternet
    Commented Apr 21, 2011 at 19:45
  • @baininternet: Will do. Thanks to the help and suggestion.
    – Scott B
    Commented Apr 28, 2011 at 17:18
  • @bainternet: This didn't work. Still shows as "Custom". 'menu-item-object-id' => $page->ID
    – Scott B
    Commented Apr 29, 2011 at 19:00

2 Answers 2

1

There's a filter:

function wpse15368_update_menu_item_type( $args )
{
    return $args['menu-item-type'] = 'page';
}
add_action( 'wp_update_nav_menu_item', 'wpse15368_update_menu_item_type' );
1
  • 1
    As @lulalala below points out, post_type is indeed the key word to use. @lulalala has the right answer.
    – surfbuds
    Commented Feb 15, 2017 at 18:14
8

Strange it maybe, `post_type' is the keyword to use:

'menu-item-object' => 'page',
'menu-item-type' => 'post_type',
'menu-item-object-id' => $post_id

This will make a menu item just like you manually created one.

1
  • 1
    This should be marked as the correct answer, thanks for sharing. Commented Apr 27, 2017 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.