Skip to content

Instantly share code, notes, and snippets.

@wbcomdev
Created October 7, 2024 09:42
Show Gist options
  • Select an option

  • Save wbcomdev/ac5a13713af6e14a6ddde3aa88579a67 to your computer and use it in GitHub Desktop.

Select an option

Save wbcomdev/ac5a13713af6e14a6ddde3aa88579a67 to your computer and use it in GitHub Desktop.
Search Businesses through Terms in search Box
<?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