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).

Restrict Access by Membership Level for CPT Categories Code Recipe Banner Image

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.

Custom Post Type Event Demo

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

Adding the Recipe to Your Website

You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.

Customize line 21 with your category name, category ID,  category slug, or array of category IDs.

Edit the pmpro_hasMembershipLevel() function with your restricted level IDs (or leave as-is to protect for all levels). See documentation on the pmpro_hasMembershipLevel function for guidance.

You can also edit line 22 to redirect to an alternative page. Or, duplicate lines 21 to 24 for your various category/redirect page goals.

Free Course: Membership Site Development—The Basics

Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.

Featured Image for Membership Site Development Course: The Basics
Was this article helpful?
YesNo