How to exclude draft pages from navigation menu in WordPress

To hide draft pages from your navigation menu, insert this code on your plugin:

/*
* Exclude draft pages from the menu
*
*/
add_filter ('wp_nav_menu_objects', 'wpster_exclude_draft_pages_from_menu', 10, 2);
function wpster_exclude_draft_pages_from_menu ($items, $args) {
 foreach ($items as $ix => $obj) {
  if (!is_user_logged_in () && 'draft' == get_post_status ($obj->object_id)) {
   unset ($items[$ix]);
  }
 }
 return $items;
}
?>

Please note that connected users will still be able to see these pages in the navigation menu.

Submit a Comment

Your email address will not be published. Required fields are marked *