Only display nav items that are published

I had a client who asked me to keep some pages set to draft so they would not be visible in the wp_nav_menu() but this did not work as expected. By default, setting a page’s status to draft after it was added to a navigation menu had no effect. So I poked around in lib/nav.php and came up with this.

I edited line 38 of lib/nav.php from this:
$output .= $item_html;

to this:

$output .= get_post_status($item->object_id) == 'publish' ? $item_html : '';

I suppose it might make for faster code execution to wrap the entire start_el() function in an if statement, but this was fine for my needs.

Rather surprised this is not default behavior, but this tidbit works like a charm. Enjoy!

Thanks for posting this. It’s good to have a solution for anyone coming across the same issue.

On a side note, I’m not surprised with the default behaviour; you would usually expect a manual setting (putting the page in the menu) to take priority over something automatic (post status).