12% off of LTD using this coupon: TWELVEPERCENTOFF. Promo ends on 2 Dec midnight UTC.
Published on May 4, 2021

Pre-Filtering CPT Items by Taxonomy in WordPress

Sridhar Katakam

In the Oxygen Facebook group a user asked:

Looking to have an archive page for a CPT (locations). Where all members will login and visit. Upon visiting the page, they should only see posts that have a custom tax that matches their membership product. 

This members-only tutorial provides the steps to

  • create a membership product using WP-Members
  • assign a membership to a user (subscriber)
  • create location Custom Post Type having a customer taxonomy

Sample location items: House 1, House 2, House 3, House 4, House 5 and House 6.

Sample taxonomy terms: Miami, New York, Tulsa.

Of these, House 3 and House 6 are tagged as Miami.

Let's say a user, miami-user has been given product access to miami membership.

When this user is viewing example.com/locations/ he/she should only see House 3 and House 6 because these CPT entries have the taxonomy term that matches the user's membership.

Step 1

Install and activate WP-Members.

Settings > WP-Members > Other Settings: Tick "Enable Products".

Configure other settings as needed.

Go to Memberships and add a new product named say, "Miami" (to follow along with the tutorial).

Step 2

Add a new test subscriber user named say miami-user.

Step 3

Add a location post type using CPT UI or otherwise.

Set Has Archive to True.

Enter locations as the post type archive slug.

Add a customer taxonomy attached to Locations.

Create a few sample taxonomy terms: Miami, New York, Tulsa.

Add a few sample location CPT items: House 1, House 2, House 3, House 4, House 5 and House 6.

Set Miami as the location for House 3 and House 6.

Note: This tutorial not only works when a CPT has one taxonomy term set, but also when more than one term has been set.

Step 4

Set up a Template that applies to location CPT archive.

when using Oxygen

If using Oxygen, add an Easy Posts component and ensure that query type is left as Default.

With a simplified Template PHP and CSS, it should show all the CPT items like this:

Step 5

Install and activate Code Snippets plugin.

Go to Snippets > Add New.

Title: Pre-Filter Locations by Customer(s)

Code:

This is a premium members-only content.

To view the rest of the content, please sign up for membership ($47/month or $599 one-time).

Already a member? Log in below or here.

tagschevron-leftchevron-rightchainangle-rightangle-upangle-downfolder-omagnifiercrossmenuchevron-downarrow-right