The code recipe below sets membership restriction on a Custom Post Type (CPT) category. When a non-member visits a CPT in a protected category (either a logged out visitor or a logged in user without membership access) they will be redirected to the membership levels page (or another page as defined in your custom function).
How it Works
You can update the code recipe with appropriate membership levels to check, the CPT categories you want to redirect, and the page that the non-member gets redirected to. You can even duplicate the recipe to account for multiple CPT category and level/redirect page relationships.
Lets say, for example, you are using an events plugin. You could set a category of event for “Members Only”, then use this recipe to redirect non-members to a sales page for membership to join these events.
Or, you may be using a directory plugin that you list special partner offers for members only. You could set a category of these “premium offer” listings that require membership.
Just want to restrict single CPTs?
If you want to restrict your Custom Post Type at the singular level, use the Custom Post Type Membership Access add on.
The Code Recipe
Select the CPTs to Restrict
The add on’s settings page detects the Custom Post Types (CPTs) active in your WordPress. You can select the CPTs to add the ‘Require Membership’ meta box to. Then on that single edit screen for the CPT select membership levels with access.
It even works with WooCommerce for “members only” products (redirects users without access from the single product view).
Then, redirect the single CPT frontend view to a selected page.
In most cases, your theme has set up CPTs to display content on the frontend in a unique way. For this reason, the standard ways that Paid Memberships Pro filters membership content will not cover all cases. Thus, the add on will simply redirect users without access to a selected page.
Most sites will want to redirect to the “membership levels” page – but you can choose any page you’d like in the drop down under Settings > PMPro CPTs.
- Upload ‘pmpro-cpt’ to the ‘/wp-content/plugins/’ directory.
- Activate the plugin through the ‘Plugins’ menu in WordPress.
- Navigate to Settings > PMPro CPTs in the WordPress Admin to select custom post types and set redirection rules.
This redirection will also apply to a search engine indexing your site.
Setting membership access restrictions for a single CPT will not necessarily hide it from archives, search, or other custom templates built into your theme.