Created
October 7, 2024 09:42
-
-
Save wbcomdev/ac5a13713af6e14a6ddde3aa88579a67 to your computer and use it in GitHub Desktop.
Search Businesses through Terms in search Box
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( 'bp_business_profile_filter', 'bp_business_profile_search_filter', 99); | |
| function bp_business_profile_search_filter( $args ) { | |
| if ( isset( $_REQUEST['search_terms'] ) && ! empty( $_REQUEST['search_terms'] ) ) { | |
| $new_args = $args; | |
| $new_args['posts_per_page'] = '-1'; | |
| $new_args['meta_query']['relation'] = 'OR'; | |
| /* | |
| * Add Multipal Meta Meta which also want to search results. | |
| * | |
| $new_args['meta_query'][] = array( | |
| 'key' => 'add meta key here', | |
| 'value' => $_REQUEST['search_terms'], | |
| 'compare' => 'LIKE', | |
| ); | |
| */ | |
| $new_args['meta_query'][] = array( | |
| 'key' => '_business_email', | |
| 'value' => $_REQUEST['search_terms'], | |
| 'compare' => 'LIKE', | |
| ); | |
| $new_args['fields'] = 'ids'; | |
| unset($new_args['s']); | |
| $business_query = new WP_Query( $new_args ); | |
| if ( $business_query->have_posts() ) { | |
| $args['post__in'] = $business_query->posts; | |
| add_filter( 'posts_where', 'bp_business_profile_custom_search_where', 10, 2 ); | |
| } | |
| } | |
| return $args; | |
| } | |
| function bp_business_profile_custom_search_where( $where, $query ) { | |
| global $wpdb; | |
| // Move the "OR" condition into the WHERE clause | |
| $where = str_replace( | |
| "AND ((({$wpdb->posts}.post_title LIKE", | |
| "OR ((({$wpdb->posts}.post_title LIKE", | |
| $where | |
| ); | |
| return $where; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment