dppb79372 2016-04-14 06:22
浏览 69
已采纳

WordPress - 删除导航菜单项时,链接的页面也在删除

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);
}
  • 写回答

1条回答 默认 最新

  • dongwei9771 2016-05-03 05:20
    关注

    This code is working for me. Please check your entire code whether there is another code or function like ,

    wp_delete_post( );
    

    added for deleting the post.

    Or try to run the query from your query browser.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题