Skip to content

Instantly share code, notes, and snippets.

@tonykwon
Last active December 21, 2015 13:19
Show Gist options
  • Save tonykwon/6312113 to your computer and use it in GitHub Desktop.
Save tonykwon/6312113 to your computer and use it in GitHub Desktop.
Hiding non-published post menu items from WordPress Menu - for post_type page and post
add_filter( 'wp_get_nav_menu_items', 'hide_non_published_menu_items', 10, 3 );
function hide_non_published_menu_items( $items, $menu, $args ) {
if ( $items ) {
foreach( $items as $k => $item ) {
// use $item->post_type if you want to apply this to custom post types
switch( $item->object ) {
case 'page':
case 'post':
if ( get_post_status( absInt($item->object_id) ) != 'publish' ) {
unset( $items[$k] );
}
break;
}
}
return $items;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment