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.

pmpro_cpt-events-demoLets 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

This code recipe requires a PMPro Plus Account or higher.

View Membership Options


Comments (3)

Hi,

At line 5, when calling the pmp_hasMembershipLevel function I’d like to fetch the list of Level assigned to that category and then pass it in as a parameter. We have a lot of membership levels and a lot of categories, so manually passing id’s doesn’t scale in the short or long term, as adding new levels would require us to update our code base.

Is there a method available for returning all levels assigned to a category? If not, if I wrote one, could it be included as part of the PMP codebase and maintained going forward so I don’t have to worry about the SQL used falling behind as PMPpro is updated?

Thanks, and a fast response appreciate, we actively are evaluating this product as a long term solution.
-Tom

I was able to tackle this a different route that worked in my instance, where I needed to redirect any post a user shouldn’t be able to access to a new template vs the default logic:

ID, NULL, false) ) {
wp_redirect(pmpro_url(‘levels’));
exit;
}
add_action(‘template_redirect’, ‘my_pmpro_term_template_redirect’);
?>

We don’t have a method, but you can get an array of level ids that have access to an array of categories like this:

`
$membership_ids = $wpdb->get_col(“SELECT membership_id FROM $wpdb->pmpro_memberships_categories WHERE category_id IN(‘” . implode(‘,’, $category_ids) . “‘”);
`

Hope this helps. If not, we can help you to write up a more specific bit of code in our member forums.

Leave a Reply

Your email address will not be published. Required fields are marked *