buddydev

Untitled

Oct 13th, 2025
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.21 KB | None | 0 0
  1.  
  2. /**
  3.  * Returns all users with a specific level.
  4.  *
  5.  * @param string $level_name Paid Membershp Pro level name.
  6.  *
  7.  * @return array
  8.  */
  9. function mpp_custom_get_pmpro_users_with_level_name( string $level_name ): array {
  10.     global $wpdb;
  11.  
  12.     // make sure pmpro is enabled.
  13.     if ( ! function_exists( 'pmpro_getAllLevels' ) ) {
  14.         return array();
  15.     }
  16.  
  17.     $user_ids = $wpdb->get_col(
  18.         $wpdb->prepare(
  19.             "SELECT mu.user_id FROM {$wpdb->pmpro_membership_levels} AS l
  20.             JOIN {$wpdb->pmpro_memberships_users} AS mu ON (l.id = mu.membership_id)
  21.                                     WHERE mu.status = 'active' AND l.name=%s", $level_name
  22.         ) );
  23.  
  24.     return array_map( 'absint', $user_ids );
  25. }
  26.  
  27.  
  28. /**
  29.  * Exclude users with the specific level from directory.
  30.  */
  31. add_filter( 'mpp_main_gallery_query_args', 'mpp_custom_filter_dir_query_args' );
  32. add_filter( 'mpp_main_gallery_dir_ajax_query_args', 'mpp_custom_filter_dir_query_args' );
  33. function mpp_custom_filter_dir_query_args( $args ) {
  34.     // not the directory page.
  35.     if ( ! empty( $args['component'] ) ) {
  36.         return $args;
  37.     }
  38.  
  39.     // CHANGE this line.
  40.     $level_name            = "Basic";
  41.     $args['exclude_users'] = mpp_custom_get_pmpro_users_with_level_name( $level_name );
  42.  
  43.     return $args;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment