• I just discovered that pages (or other posttypes) that have a post_status ‘draft’ or ‘pending’ are still showing up with the wp_nav_menu() function.

    For me this doesnt make sense and I created the following solution:

    Add this to your Themes functions.php

    function exclude_draft_nav_items( $items, $menu, $args )
    {
    	global $wpdb;
    
    	//add your custom posttypes to this array
    	$allowed_posttypes = array( 'post', 'page' );
    
    	$sql = "SELECT ID FROM {$wpdb->prefix}posts WHERE (post_status='draft' OR post_status='pending') AND ID=%d && post_type=%s";
    
    	foreach ( $items as $k => $item )
    	{
    		if( in_array($item->object, $allowed_posttypes) )
    		{
    			$query = $wpdb->prepare( $sql, $item->object_id, $item->object );
    			$result = $wpdb->get_var( $query );
    
    			if( $result ) unset($items[$k]);
    		}
    	}
    
    	return $items;
    }
    add_filter( 'wp_get_nav_menu_items', 'exclude_draft_nav_items', 10, 3 );
  • The topic ‘solution for draft pages and wp_nav_menu’ is closed to new replies.