i am looking for solution to hide (just not print) menu items if the page content is empty.

by default wordpress create menu item when new page created. and that ok.

but what i don't want, is when new page created, and the user still did not add any content to the page, is to show the page in the menu.

any suggestions?

  • I think page will never empty. Because every page have at-least default header and footer. Dec 2, 2014 at 12:05
  • @HelpingHands you are right. but i want to check if the body content is empty and then to hide the menu item.
    – need-help
    Dec 2, 2014 at 12:13
  • I think that is not feasible still not sure may be some one can help here. You can create custom menu to achieve your goal. If page have not content then you can delete that page from menu otherwise keep it. Dec 2, 2014 at 12:16
  • In default themes, WordPress falls back to wp_page_menu if a nav menu isn't set up. The intention is for you to create a menu yourself so only pages you explicitly add show up in the menu.
    – Milo
    Dec 2, 2014 at 17:03

1 Answer 1


You could try this, loop through all the wp_nav_menu items, get the post using the Object ID then test to see IF the content is empty. IF that content is empty then unset it from the items array:

function remove_empty_items( $items ) {
    $blog_page = get_option( 'page_for_posts' );
    $home_page = get_option( 'page_on_front' );
    $postArr   = get_posts ( 'post_type=post' );

    foreach( $items as $key => $item ) {
        $tmpPost = get_post( $item->object_id );

        if( $item->object_id == $blog_page && empty( $postArr ) ) {
            unset( $items[$key] );
        } else if( $item->object_id !== $home_page && empty( $tmpPost->post_content ) ) {
            unset( $items[$key] );

    return $items;
add_filter( 'wp_get_nav_menu_items', 'remove_empty_items' );
  • Hi @Howdy_McGee yes thats help. Thanks! but i see another problem. i have page the show all posts(blog page) and this page dont removed from the loop if the user has not created any posts. so i can get this page by: $page_for_posts = get_option( 'page_for_posts' ); but how i can check if there is no posts, remove the page?
    – need-help
    Dec 3, 2014 at 6:56
  • @need-help You would have to run a small query just to check if there were any posts in that post type - I've updated my answer to reflect the comment.
    – Howdy_McGee
    Dec 3, 2014 at 14:41
  • Hi @Howdy_McGee i take your code and insert in funcastion.php and the website get white page. so somthing not working.
    – need-help
    Dec 3, 2014 at 20:44
  • @need-help whoops, try that - blog_post wasn't a variable.
    – Howdy_McGee
    Dec 3, 2014 at 20:47
  • Hi @Howdy_McGee great the code works but with bug. it's also delete the home page from menu item. i have set some of the page as static page, and yes this page have body content. any suggestion?
    – need-help
    Dec 3, 2014 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.