Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active April 14, 2024 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save strangerstudios/8a2ae1695e61f2a088af4b0f6536b452 to your computer and use it in GitHub Desktop.
Save strangerstudios/8a2ae1695e61f2a088af4b0f6536b452 to your computer and use it in GitHub Desktop.
Add specific members-only categories back to main loop when filtering searches and archives.
<?php
function my_pmpro_category_filter($query)
{
//get the members-only hidden categories
$hidden_cat_ids = $query->query_vars['category__not_in'];
//set the members-only category IDs NOT to filter
$not_hidden_cat_ids = array('1','10');
//add hidden category IDs back to the query
foreach($not_hidden_cat_ids as $not_hidden_cat_id)
{
if(($key = array_search($not_hidden_cat_id, $hidden_cat_ids)) !== false) {
unset($hidden_cat_ids[$key]);
}
}
//set the updated hidden category IDs
$query->set('category__not_in', $hidden_cat_ids);
}
$filterqueries = pmpro_getOption("filterqueries");
if(!empty($filterqueries))
add_filter( 'pre_get_posts', 'my_pmpro_category_filter');
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Advanced Category Techniques for Filtering Searches and Archives to Members and Non-Members" at Paid Memberships Pro here: https://www.paidmembershipspro.com/advanced-category-techniques-filtering-searches-archives-members-non-members/

@ideadude
Copy link

This gist no longer works with PMPro 3.0+. You can find an updated version of this gist here: https://gist.github.com/ideadude/4ebc6abe1c88bd1a38198a60b883eea9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment