WordPress: Don’t Show Drafts
I have come across a theme that sometimes displays posts and menu drafts on the front end. This code below will stop those pesky drafts from appearing in any wordpress loop on the front end! Just place it in your themes functions.php file within your tags.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
/*Don't Show Drafts*/ 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 ); |
Recent Comments