pmpro_search_filter

The Paid Memberships Pro pmpro search filter function.

Description

pmpro_search_filter( $query ); 

Parameters (1)

0. $query
The query.

Usage

  1. if ( !function_exists( 'pmpro_search_filter' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'paid-memberships-pro/includes/content.php'; 
  3.  
  4. // The query. 
  5. $query = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = pmpro_search_filter($query); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/content.php  
  1. function pmpro_search_filter($query) 
  2. global $current_user, $wpdb, $pmpro_pages; 
  3.  
  4. //hide pmpro pages from search results 
  5. if(!$query->is_admin && $query->is_search && empty($query->query['post_parent'])) 
  6. if(empty($query->query_vars['post_parent'])) //avoiding post_parent queries for now  
  7. $query->set('post__not_in', $pmpro_pages ); 
  8.  
  9. $query->set('post__not_in', $pmpro_pages ); // id of page or post  
  10.  
  11. //hide member pages from non-members (make sure they aren't hidden from members)  
  12. if(!$query->is_admin && 
  13. !$query->is_singular &&  
  14. empty($query->query['post_parent']) && 
  15. empty($query->query_vars['post_type']) ||  
  16. in_array($query->query_vars['post_type'], apply_filters('pmpro_search_filter_post_types', array("page", "post"))) 
  17. )  
  18. {  
  19. //get page ids that are in my levels 
  20. if(!empty($current_user->ID)) 
  21. $levels = pmpro_getMembershipLevelsForUser($current_user->ID); 
  22. else 
  23. $levels = false; 
  24. $my_pages = array(); 
  25. $member_pages = array(); 
  26.  
  27. if($levels) { 
  28. foreach($levels as $key => $level) { 
  29. //get restricted posts for level 
  30.  
  31. // make sure the object contains membership info. 
  32. if (isset($level->ID)) { 
  33.  
  34. $sql = $wpdb->prepare(" 
  35. SELECT page_id 
  36. FROM {$wpdb->pmpro_memberships_pages} 
  37. WHERE membership_id = %d",  
  38. $level->ID 
  39. ); 
  40.  
  41. $member_pages = $wpdb->get_col($sql); 
  42. $my_pages = array_unique(array_merge($my_pages, $member_pages)); 
  43. } // foreach 
  44. } // if($levels) 
  45.  
  46. //get hidden page ids 
  47. if(!empty($my_pages)) 
  48. $sql = "SELECT page_id FROM $wpdb->pmpro_memberships_pages WHERE page_id NOT IN(" . implode(', ', $my_pages) . ")"; 
  49. else 
  50. $sql = "SELECT page_id FROM $wpdb->pmpro_memberships_pages"; 
  51. $hidden_page_ids = array_values(array_unique($wpdb->get_col($sql)));  
  52.  
  53. if($hidden_page_ids) 
  54. if(empty($query->query_vars['post_parent'])) //avoiding post_parent queries for now  
  55. $query->set('post__not_in', $hidden_page_ids); 
  56.  
  57. //get categories that are filtered by level, but not my level 
  58. global $pmpro_my_cats; 
  59. $pmpro_my_cats = array(); 
  60.  
  61. if($levels) { 
  62. foreach($levels as $key => $level) { 
  63. $member_cats = pmpro_getMembershipCategories($level->id); 
  64. $pmpro_my_cats = array_unique(array_merge($pmpro_my_cats, $member_cats)); 
  65.  
  66. //get hidden cats 
  67. if(!empty($pmpro_my_cats)) 
  68. $sql = "SELECT category_id FROM $wpdb->pmpro_memberships_categories WHERE category_id NOT IN(" . implode(', ', $pmpro_my_cats) . ")"; 
  69. else 
  70. $sql = "SELECT category_id FROM $wpdb->pmpro_memberships_categories"; 
  71.  
  72. $hidden_cat_ids = array_values(array_unique($wpdb->get_col($sql))); 
  73.  
  74. //make this work 
  75. if($hidden_cat_ids) 
  76. {  
  77. $query->set('category__not_in', $hidden_cat_ids); 
  78.  
  79. //filter so posts in this member's categories are allowed 
  80.  
  81. return $query;