Created
July 17, 2026 14:25
-
-
Save Crocoblock/b74ca577490dc4f00d28ffceb3842d88 to your computer and use it in GitHub Desktop.
Search Filter by first and last name and by user ID
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 | |
| add_filter( 'jet-smart-filters/query/final-query', function( $query ) { | |
| if ( empty( $query['meta_query'] ) ) { | |
| return $query; | |
| } | |
| foreach ( $query['meta_query'] as $index => $meta_query_item ) { | |
| // Check for our custom marker key (e.g., user_prop__multi_search) | |
| if ( isset( $meta_query_item['key'] ) && false !== strpos( $meta_query_item['key'], 'user_prop__multi_search' ) ) { | |
| $search_value = $meta_query_item['value']; | |
| // 1. Configure search by standard database columns (including ID) | |
| $query['search'] = '*' . $search_value . '*'; | |
| $query['search_columns'] = array( 'ID', 'user_login', 'user_nicename' ); | |
| // 2. Add search by metafields (first_name and last_name) using OR relation | |
| $query['meta_query'][] = array( | |
| 'relation' => 'OR', | |
| array( | |
| 'key' => 'first_name', | |
| 'value' => $search_value, | |
| 'compare' => 'LIKE', | |
| ), | |
| array( | |
| 'key' => 'last_name', | |
| 'value' => $search_value, | |
| 'compare' => 'LIKE', | |
| ), | |
| ); | |
| // Remove the temporary marker so it doesn't break the main query | |
| unset( $query['meta_query'][$index] ); | |
| } | |
| } | |
| return $query; | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment