Have you ever tried to set a WordPress page to draft status, after
However, when I viewed the webpage as a guest, I could still see the menu item. When I opened that page WordPress only displays a 404 not-found message. Which is technically correct; but wouldn’t it be a lot better, when guest users would not even see the menu items for draft posts?
The complicated way
Obviously, WordPress wants us to modify the post state and separately update the menu (remove the page from the menu manually). Possibly this is a good idea for caching – but on the other hand, most caching is usually disabled for logged in users anyway.
A more elegant way
Or maybe this is just a lazy way.
I took a look into the file
nav-menu-template.php in search of a solution that does the redundant work and modifies the main menu. We hook into the filter
wp_nav_menu_objects so we can remove inaccessible menu-items right before they are displayed!
Actually, it’s a straight forward process, once we found the correct hook and understand the filter-parameter: