WordPress Custom Menus become “Pending” when I try to run following code

Here I am changing status of my Published posts to draft where post have less content using following code

function draftpost(){
    global $wpdb;
    $prefix = $wpdb->prefix;
    $wpdb->query("UPDATE ".$prefix."posts SET post_status='draft' WHERE LENGTH( post_content ) <500");
}
add_action('publish_post', 'draftpost');

This code is working Fine for changing Published Items to Draft.

Read More

Issue is that this code removes my Custom Menu and make all Items in Menu to PENDING. How can I solve this?

Related posts

Leave a Reply

1 comment

  1. You need to extend your query.

    $wpdb->query("UPDATE ".$prefix."posts SET post_status='draft' WHERE LENGTH( post_content ) < 500 AND post_type = 'post'");
    

    Your query overwrites all post_types, including nav_menu_item.