I am trying to develop a custom plugin through which I have to add and delete new items to my nav menu.
And I have saved the term_object_id
from wp_term_relationships
of each menu tem.
When trying to remove an item from menu it also deleting the linked page , I am trying to delete the menu item using the following function,
function removeMenuEntry($menu_id,$menuterm){
$itemData = array(
'menu-item-object-id' => $menuterm,
'menu-item-status' => 'draft'
);
wp_update_nav_menu_item($menu_id, $menuterm, $itemData) ;
}
I also tried with the follwing method, but same result,
function removeMenuItem($term_menu){
global $wpdb;
$sql = " DELETE FROM wp_term_relationships WHERE object_id = '$term_menu' ; ";
$wpdb->query($sql);
$sql = "
DELETE FROM wp_posts
WHERE ID=(
SELECT post_id from wp_postmeta
WHERE meta_key='_menu_item_object_id'
AND meta_value='$term_menu'
) AND post_type='nav_menu_item' ; ";
$wpdb->query($sql);
$sql = " SELECT post_id from wp_postmeta
WHERE meta_key='_menu_item_object_id'
AND meta_value='$term_menu' ";
$result_state = $wpdb->get_results($sql,ARRAY_A);
$sql = "DELETE from wp_postmeta
WHERE post_id=".$result_state[0]['post_id']." ";
$wpdb->query($sql);
}