Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating a script that automates build a wordpress site from another CMS. I've been able to change every setting: themes, options, subdomain, site title, etc.

But what is eluding me is being able to create custom menus. The code below SHOULD be able to do it for me. However it is not, and I am completely stumped on what to do.

This code is not being executed in the admin panel (Its not a plugin). Its actually sitting on top of wordpress and includes wp-load and wp-admin functions pages.

$mymenu = wp_get_nav_menu_object("Main Navigation Menu");
$menuID = (int) $mymenu->term_id;
$itemData = array(
    'menu-item-db-id' => 0,
    'menu-item-object-id' => $pageId,
    'menu-item-object' => 'page',
    'menu-item-type'  => 'post_type',
    'menu-item-parent-id' => 0,
    'menu-item-position' => $itemOrder,
    'menu-item-title' => $pageData['title'],
    'menu-item-url' => get_permalink($pageId),
    'menu-item-description' => $pageData['post_content'],
    'menu-item-attr-title' => $pageData['post_excerpt'],
    'menu-item-status' => 'publish',<br />
    'menu-item-target' => ''<br />
);
$thisMenuItem = wp_update_nav_menu_item($menuID, 0, $itemData);

$itemData is the result of data coming in from another CMS plus the result of saving it off a post in wordpress to produce the menu item that I want in my menu. Any help on getting the wp_update_nav_menu_item to save would be very helpful. Again this is creating a custom menu without the use of the admin panel.`

share|improve this question
    
Small update to this. I know that the items are getting into the database and saving as post_type nav_menu_item. However they are not being connecting to their parent. –  Paul Nov 20 '12 at 22:17
    
Are you using WordPress Multisite? Just to understand why you titled and tagged like that and are not mentioning it in the content... –  brasofilo Nov 20 '12 at 22:49
    
Yes I am. Sorry I forgot to mention it, but I finally figured out my issues late last night. –  Paul Nov 21 '12 at 23:20

2 Answers 2

While Paul's answer was really helpful and definitely put me in the right direction, his code didn't quite work for my issue.

Basically, the function wp_get_nav_menu_object() returns the $menu object which has (among others) term_id and term_taxonomy_id properties.

On my clients WP instance term_id and term_taxonomy_id weren't the same.

What Paul does is insert the term_id property in the relationship table and link it to term_taxonomy_id column, but in my case I had to insert the term_taxonomy_id property from the $menu object (and looking at the naming - I believe this is proper).

Here's the link if you want to check up on the difference between term_id and term_taxonomy_id.

So in the end, my code ended up looking like this:

$wpdb->insert(
    $wpdb->term_relationships, 
    array(
        "object_id"        => $menu_item_id, 
        "term_taxonomy_id" => $menu->term_taxonomy_id
    ), 
    array(
        "%d", 
        "%d"
    )
);
share|improve this answer
up vote 1 down vote accepted
$wpdb->insert( 
    $wpdb->term_relationships, 
    array(
        "object_id" => $thisMenuItem, 
        "term_taxonomy_id" => $menuID
    ), 
    array( "%d", "%d" ) 
);

This was missing from the Question code.
It allows the menu items to bind to the navigation menu itself.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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