Halloween Sale 15% off Ends in See the plans
PHP

Members-Only Post Type

Sends logged-out visitors to the login page when they open entries or the archive of a chosen post type, and hides those entries from them in the REST API.

Don’t have WPIDE yet? Install it on your site

Code

// Post types only logged-in users may see.
$wpide_members_types = array( 'resource' );

add_action( 'template_redirect', function () use ( $wpide_members_types ) {
    if ( is_user_logged_in() || ! ( is_singular( $wpide_members_types ) || is_post_type_archive( $wpide_members_types ) ) ) {
        return;
    }

    $requested = home_url( add_query_arg( array() ) );
    wp_safe_redirect( wp_login_url( $requested ) );
    exit;
} );

foreach ( $wpide_members_types as $wpide_members_type ) {
    add_filter( "rest_{$wpide_members_type}_query", function ( $args ) {
        if ( ! is_user_logged_in() ) {
            $args['post__in'] = array( 0 );
        }

        return $args;
    } );
}

add_filter( 'rest_request_before_callbacks', function ( $response, $handler, $request ) use ( $wpide_members_types ) {
    if ( is_user_logged_in() || is_wp_error( $response ) ) {
        return $response;
    }

    foreach ( $wpide_members_types as $type ) {
        $object = get_post_type_object( $type );

        if ( $object && $object->show_in_rest && 0 === strpos( $request->get_route(), '/' . ( $object->rest_namespace ?: 'wp/v2' ) . '/' . ( $object->rest_base ?: $type ) . '/' ) ) {
            return new WP_Error( 'rest_forbidden', 'Please log in to view this content.', array( 'status' => 401 ) );
        }
    }

    return $response;
}, 10, 3 );

add_action( 'pre_get_posts', function ( $query ) use ( $wpide_members_types ) {
    // Keep these entries out of search results and feeds for guests.
    if ( is_admin() || is_user_logged_in() || ! ( $query->is_search() || $query->is_feed() ) || ! $query->is_main_query() ) {
        return;
    }

    $types = $query->get( 'post_type' );

    if ( $types && 'any' !== $types ) {
        $query->set( 'post_type', array_values( array_diff( (array) $types, $wpide_members_types ) ) );
    } else {
        $query->set( 'post_type', array_values( array_diff( get_post_types( array( 'exclude_from_search' => false ) ), $wpide_members_types ) ) );
    }
} );

Notes

After logging in, visitors return to the page they wanted. Combine with noindex if the content was public before.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.