# Hide Members-Only Pages From Menus for Visitors

> Removes menu links to posts and pages marked members-only (by the WPIDE "Members-Only Posts" snippet) or set to private, so visitors never see links they can't open.

- Type: PHP
- Runs: Frontend only
- Category: [Memberships](https://wpide.com/library/category/memberships/)
- Tags: menus, members, private
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/hide-members-only-menu-items/

## Code

```php
add_filter( 'wp_nav_menu_objects', function ( $items ) {
    if ( is_user_logged_in() ) {
        return $items;
    }

    $removed = array();

    foreach ( $items as $key => $item ) {
        $hide = in_array( (int) $item->menu_item_parent, $removed, true );

        if ( ! $hide && 'post_type' === ( $item->type ?? '' ) && ! empty( $item->object_id ) ) {
            $object_id = (int) $item->object_id;
            $hide      = 'private' === get_post_status( $object_id ) || (bool) get_post_meta( $object_id, '_wpide_members_only', true );
        }

        if ( $hide ) {
            $removed[] = (int) $item->ID;
            unset( $items[ $key ] );
        }
    }

    return $items;
} );
```

## Notes

Logged-in users still see every link; restrictions by role are checked by the members-only snippet itself when the page is opened.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Hide Members-Only Pages From Menus for Visitors" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
