Last active
August 8, 2025 07:40
-
-
Save cryptexvinci/c87518f39a36fba92a8f99414d2d5b12 to your computer and use it in GitHub Desktop.
Ultimate Member - Exclude a specific field from the search results in the Ultimate Member directory
This file contains hidden or 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 | |
| /** | |
| * Exclude a specific field from the search results in the Ultimate Member directory | |
| * | |
| * This code prevents the 'referred_by' field from being included when users | |
| * search through the members directory, while still allowing other fields | |
| * to be searched normally. | |
| */ | |
| /** | |
| * Filter the meta query for general search to exclude specific field | |
| * | |
| * @param array $meta_query The current meta query array | |
| * @param string $search The search term entered by the user | |
| * @return array Modified meta query excluding the specified field | |
| */ | |
| add_filter('um_member_directory_general_search_meta_query', function($meta_query, $search) { | |
| // Define the field key to exclude from search results | |
| $exclude_key = 'referred_by'; | |
| // Only modify if there's an existing meta query | |
| if (!empty($meta_query)) { | |
| $meta_query = [ | |
| 'relation' => 'AND', | |
| [ | |
| // Exclude the specified field from search | |
| 'relation' => 'OR', | |
| [ | |
| 'key' => $exclude_key, | |
| 'value' => $search, | |
| 'compare' => 'NOT LIKE', | |
| ], | |
| [ | |
| // Also exclude users where this field doesn't exist | |
| 'key' => $exclude_key, | |
| 'compare' => 'NOT EXISTS', | |
| ], | |
| ], | |
| [ | |
| // Allow normal search functionality for other fields | |
| 'relation' => 'OR', | |
| [ | |
| 'value' => $search, | |
| 'compare' => '=', | |
| ], | |
| [ | |
| 'value' => $search, | |
| 'compare' => 'LIKE', | |
| ], | |
| [ | |
| // Handle serialized data search | |
| 'value' => serialize((string) $search), | |
| 'compare' => 'LIKE', | |
| ], | |
| ], | |
| ]; | |
| } | |
| return $meta_query; | |
| }, 20, 2); | |
| /** | |
| * Modify the database JOIN query to exclude specific field from search | |
| * | |
| * This filter modifies the SQL JOIN statement to prevent the excluded field | |
| * from being included in the database search query. | |
| * | |
| * @param mixed $additional_search Additional search parameters (unused in this context) | |
| * @return mixed Returns the original additional_search parameter | |
| */ | |
| add_filter('um_member_directory_meta_general_search_meta_query', function($additional_search) { | |
| global $wpdb; | |
| // Define the field key to exclude from search results | |
| $exclude_key = 'referred_by'; | |
| // Check if there are existing JOIN queries to modify | |
| if (!empty(UM()->member_directory()->joins)) { | |
| foreach (UM()->member_directory()->joins as $k => $value) { | |
| // Original JOIN query that searches all metadata | |
| $search_query = "LEFT JOIN {$wpdb->prefix}um_metadata umm_search ON umm_search.user_id = u.ID"; | |
| // Modified JOIN query that excludes our specific field | |
| $replace_query = "LEFT JOIN {$wpdb->prefix}um_metadata umm_search ON (umm_search.user_id = u.ID AND umm_search.um_key != '{$exclude_key}')"; | |
| // Replace the original query with our modified version | |
| if ($search_query === $value) { | |
| UM()->member_directory()->joins[$k] = $replace_query; | |
| } | |
| } | |
| } | |
| return $additional_search; | |
| }, 20, 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment