WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_update_nav_menu_item not saving (6 posts)

  1. bosspj
    Member
    Posted 1 year ago #

    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.

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

    $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.</p>
    <p>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.`

  2. bosspj
    Member
    Posted 1 year ago #

    Going through the code some more, I've found out that the term_relationship table is not being updated correctly. It is completely missing all of the nav_menu items that are saving inside of the post table

  3. bosspj
    Member
    Posted 1 year ago #

    Found the answer to this

    $wpdb->insert($wpdb->term_relationships, array("object_id" => $thisMenuItem, "term_taxonomy_id" => $menuID), array("%d", "%d"));

    Needed to create the relationship in the database directly. Apparently this is done some where in the saving menu process in the admin. Not sure where, but this did fix it for me.

  4. karrikas
    Member
    Posted 1 year ago #

    Hi bosspj,

    I had a similar problem. I created a plugin with an ajax page. In this ajax page I put "wp_update_nav_menu_item". All is correct if I'm logged. But If I don't logged I have your problem.

    Thank you for the solution,

  5. dfcowell
    Member
    Posted 1 year ago #

    Thanks bosspj, this is really helpful. :) I struggled with the same issue until I found your solution.

  6. yeshwanth
    Member
    Posted 1 year ago #

    thanks bosspj, that helped me a lot, searching for this from last 10 hours,
    i got a question that,
    wp_update_nav_menu_item should update the DB, here we are performing the
    manual DB insert which is unusual and unsafe,
    is there anything missing from Wp-update_nav_menu_item, so that we can get rid of the wpdb insert,

    and i got another issue that, how do i delete an item from Menu.

    thanks for the post and looking for Updations.

Topic Closed

This topic has been closed to new replies.

About this Topic