Created
March 20, 2024 15:53
-
-
Save unwiredtech/3b57176a4e86c4dba7bf41d9df591e8b to your computer and use it in GitHub Desktop.
Adds teh metafield as additional search source for Wordpress Search / JetSmartFilter - Wordpress Search
This file contains 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
/* Dev500 - Patrick | |
* March 19, 2024 | |
* @functions.php | |
* @page /?s | |
* Adds teh metafield as additional search source for Wordpress Search / JetSmartFilter - Wordpress Search | |
*/ | |
// Modify the main WordPress query | |
function custom_search_filter($query) { | |
if (!is_admin() && $query->is_main_query() && $query->is_search()) { | |
// Include custom meta fields in search | |
$meta_query = array(); | |
// Meta fields for each custom post type | |
$custom_meta_fields = array( | |
'experts' => array('position', 'work-location', 'market-expertise', 'product-expertise', 'service-expertise'), | |
'insights' => array('products-and-services-box-1-title', 'products-and-services-box-2-title', 'products-and-services-box-3-title'), | |
'podcasts' => array('after-title', 'introduction', 'featured-guest-content', 'episode-number'), | |
'case_studies' => array('overview', 'service-offered-repeater', 'approach-and-solution-content', 'impact'), | |
'top-master-pc' => array('under-title-text'), | |
'infographics' => array('related-article'), | |
'in_the_news' => array('news-source') | |
); | |
// Get the current post type | |
$post_type = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : ''; | |
// If the post type is one of your custom post types, include its meta fields in search | |
if (array_key_exists($post_type, $custom_meta_fields)) { | |
foreach ($custom_meta_fields[$post_type] as $meta_field) { | |
$meta_query[] = array( | |
'key' => $meta_field, | |
'value' => get_search_query(), | |
'compare' => 'LIKE' | |
); | |
} | |
} | |
// Combine title, content, and meta field search | |
$query->set('meta_query', $meta_query); | |
$query->set('meta_key', ''); // Ensure meta_key is empty to search all meta fields | |
} | |
} | |
add_action('pre_get_posts', 'custom_search_filter'); | |
// Modify the search SQL query to include custom meta fields | |
function custom_search_join($join) { | |
global $wpdb; | |
if (is_search() && !is_admin()) { | |
$join .= " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) "; | |
} | |
return $join; | |
} | |
add_filter('posts_join', 'custom_search_join'); | |
function custom_search_where($where) { | |
global $wpdb; | |
if (is_search() && !is_admin()) { | |
$where = preg_replace( | |
"/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", | |
"(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", | |
$where | |
); | |
} | |
return $where; | |
} | |
add_filter('posts_where', 'custom_search_where'); | |
function custom_search_groupby($groupby) { | |
global $wpdb; | |
if (is_search() && !is_admin()) { | |
$groupby = "$wpdb->posts.ID"; | |
} | |
return $groupby; | |
} | |
add_filter('posts_groupby', 'custom_search_groupby'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment