Last active
August 29, 2015 14:02
-
-
Save rayflores/a3a0f19b5f81b1f8d3ec to your computer and use it in GitHub Desktop.
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
<?php | |
function justin_search_join( $join ) { | |
global $wpdb; | |
if( is_search() && !is_admin()) { | |
$join .= "LEFT JOIN $wpdb->postmeta AS m ON ($wpdb->posts.ID = m.post_id) "; | |
} | |
return $join; | |
} | |
// uncomment this to add this functionality | |
//add_filter('posts_join', 'justin_search_join' ); | |
function justin_search_groupby( $groupby ) { | |
global $wpdb; | |
if( is_search() && !is_admin()) { | |
$groupby = "$wpdb->posts.ID"; | |
} | |
return $groupby; | |
} | |
// uncomment this to add this functionality | |
//add_filter('posts_groupby', 'justin_search_groupby' ); | |
function justin_search_where( $where ) { | |
global $wpdb, $wp_query; | |
if( is_search() && !is_admin()) { | |
$where = ""; | |
$search_terms = se_get_search_terms(); | |
$n = !empty($wp_query->query_vars['exact']) ? '' : '%'; | |
$searchand = ''; | |
if (count($search_terms) < 1) { | |
// no search term provided: so return no results | |
$search = "1=0"; | |
} else { | |
foreach( $search_terms as $term ) { | |
$term = esc_sql( like_escape( $term ) ); | |
$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}') OR (m.meta_value LIKE '{$n}{$term}{$n}'))"; | |
$searchand = ' AND '; | |
} | |
} | |
$where .= " AND ${search} "; | |
$where .= " AND (m.meta_key IN ('_ENTER_KEY_HERE')) "; | |
$where .= " AND ($wpdb->posts.post_password = '') "; | |
$where .= " AND ($wpdb->posts.post_type IN ('post', 'page', '__ENTER_CUSTOM_POST_TYPE_HERE')) "; | |
$where .= " AND ($wpdb->posts.post_status = 'publish') "; | |
} | |
return $where; | |
} | |
// uncomment this to add this functionality | |
//add_filter('posts_where', 'justin_search_where' ); | |
// Code from Search Everywhere plugin | |
function se_get_search_terms() | |
{ | |
global $wpdb, $wp_query; | |
$s = isset($wp_query->query_vars['s']) ? $wp_query->query_vars['s'] : ''; | |
$sentence = isset($wp_query->query_vars['sentence']) ? $wp_query->query_vars['sentence'] : false; | |
$search_terms = array(); | |
if ( !empty($s) ) | |
{ | |
// added slashes screw with quote grouping when done early, so done later | |
$s = stripslashes($s); | |
if ($sentence) | |
{ | |
$search_terms = array($s); | |
} else { | |
preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches); | |
$search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]); | |
} | |
} | |
return $search_terms; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment