Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function custom_search_where($pieces) {
- // filter to select search query
- if (is_search() && !is_admin()) {
- global $wpdb;
- $custom_fields = array('your_custom_field', 'your_other_custom_field');
- $keywords = explode(' ', get_query_var('s'));
- $query = "";
- foreach ($custom_fields as $field) {
- foreach ($keywords as $word) {
- $query .= "((mypm1.meta_key = '".$field."')";
- $query .= " AND (mypm1.meta_value LIKE '%{$word}%')) OR ";
- }
- }
- if (!empty($query)) {
- // add to where clause
- $pieces['where'] = str_replace("(((wp_posts.post_title LIKE '%", "( {$query} ((wp_posts.post_title LIKE '%", $pieces['where']);
- $pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
- }
- }
- return ($pieces);
- }
- add_filter('posts_clauses', 'custom_search_where', 20, 1);
- /*
- ** Code from:
- ** http://wpdevsnippets.com/extend-search-include-custom-fields-without-plugin/
- */
- // ******************************************************
- // IMPORTANT: prevent duplicate posts being returned
- // ******************************************************
- function websmart_search_groupby( $groupby ) {
- global $wpdb;
- if( is_search() && !is_admin()) {
- $groupby = "$wpdb->posts.ID";
- }
- return $groupby;
- }
- add_filter('posts_groupby', 'websmart_search_groupby' );
- /*
- ** Code from:
- ** http://websmartdesign.co.nz/searching-structured-post-data-with-wordpress/
- */
Advertisement
Add Comment
Please, Sign In to add comment