If you’re adding membership to an existing WordPress site with a lot of content, chances are you are looking for a way to speed up the initial setup process of restricting content throughout your site.

This post offers developers a few advanced methods to set up restrictions in bulk via MySQL.

Banner Image for Bulk Restrictions by Level via MYSQL

Before Getting Started

The recipes below include a series of MySQL commands that you will run in your WordPress site’s database. Most hosts allow you to access your database via the hosting dashboard. There are also a few plugins that allow you to access the site’s database directly through the WordPress dashboard.

It is very important to take a new backup when you are doing any database work. You can review this post for some guidelines on how to set up a database backup process for your site.

The choice to use these commands depends on the type of content you are trying to restrict. For example, if you have a specific post category you would like to now make private, you can simply restrict access to that post category by editing the Membership Level under Memberships > Settings > Membership Levels. This step circumvents the need for running a bulk database update as it will immediately restrict all posts in those categories.

Alternately, if your entire site will now become members-only, you can use the method outlined in this article to restrict access to your entire site. Only the ‘okay pages’ (pages required for all visitors to compare and select a membership level for checkout) are left public.

Basic Steps for Running a MySQL Command

  1. Access your site’s MySQL Database via phpMyAdmin. phpMyAdmin is a visual tool that allows you to navigate, browse, and modify your site’s database in a friendly interface. Your hosting panel may provide a direct link to access phpMyAdmin or you can try some of these plugins that allow you to access the site’s database directly through the WordPress dashboard.
  2. Navigate to the wp_pmpro_memberships_pages table. Note that your table may have a different prefix than wp.
  3. This table is where PMPro stores the membership_id to page_id relationship that restricts a single post, page, CPT, and more. The commands below will insert new rows into this table.
  4. Locate the view in phpMyAdmin to run a SQL command. Every phpMyAdmin interface is slightly different depending on the version used and active skin of the panel. You’re looking for a view that has a large input area to paste in a SQL command.
  5. Paste in the appropriate SQL query.
  6. Click to execute the query. Some phpMyAdmin interfaces allow you to run a command as a test drive, while others only allow you to perform the command. Ensure you have a proper backup of your database before taking any of these actions. (I’ve said this twice now. You are sufficiently warned.)
  7. After this, your query is complete. The command has been run, and a displayed message will show how many posts were affected. You can run additional commands for other level IDs or post types by repeating these steps.
  8. As a final step, try to access a piece of content your command should have restricted as a non-member. This will confirm that the appropriate content was locked as part of your database update.

The remaining portion of this article shares a few methods to bulk restrict posts, pages, and other post types. Note that these recipes assume your WordPress site’s database prefix is wp. If you are using another prefix for your tables, be sure to update the query before running it.

Bulk Restricting Posts

In general, anything of the ‘post’ post_type is the content displayed in your site’s blog. These are the categorized articles that show up in the archive or the ‘loop’ view. This recipe will restrict access to all ‘posts’ for level ID 2.

Customizing the recipe: You can alter the level ID (in this recipe, it is locking all posts for level ID 2) for your site needs.

Bulk Restricting Pages

Many pages in your WordPress site should not be restricted (and applying membership requirements to certain pages can result in redirect loops and unintended errors). In general, it is important that you do not set any membership requirements on your homepage and all of the pages generated for PMPro (you can see what pages are required and “okay” under Memberships > Settings > Page Settings—don’t restrict any of these pages).

The recipe below will replicate parent page membership requirements across all child pages in your site. For this recipe to work, you must first add level requirements via the Pages > Edit Page > “Require Membership” metabox to the appropriate parent pages.

Alternately, the recipe below will lock all pages that are a child of a specific page (in this case, page ID 123).

Customizing the recipe: You can alter the parent page ID (in this recipe, it is locking all child pages of post ID 123) for your site needs.

Bulk Restricting Custom Post Types

If you are using the Custom Post Type Membership Access Add On to restrict CPT access, this recipe will help you speed up the initial set up but replicating membership requirements across all CPTs of a selected post_type for a specific level.

Customizing the recipe: You can alter the level ID (in this recipe, it is locking CPTs for level ID 3) and the post_type value for your site needs.

Duplicating Membership Requirements Across Levels

Once you have some posts, pages, or CPTs restricted for a specific level, you can use this recipe to duplicate restrictions from one level to also apply to another level. The recipe below duplicates all membership requirements for anything locked to membership level ID 1 to also apply to membership level ID 2.

Customizing the recipe: You can alter the level IDs ‘2’ (new content to lock) and ‘1’ (already locked content to replicate) in this recipe for your site needs.

Need another bulk MySQL command?

Check out the Developers Toolkit Add On for some other scripts to duplicate membership restrictions from one level to another.

If you need something more custom, please open a ticket in the premium support area and we will guide you in preparing the custom MySQL command.

Bulk Assign Levels to Posts

If you would like to bulk assign levels to posts and do not want to go the MySQL route, you could use the following code recipe. Adjust the code recipe’s $levels and $post_type to suit your needs.

Once the code recipe is added and saved to your website, add the following to the end of your URL in order to run the script /wp-admin/?assign_levels_posts=true

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.

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