Last active
April 14, 2024 16:20
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); |
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
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/