Skip to content

Instantly share code, notes, and snippets.

@jonbish
Last active December 17, 2015 14:59
Show Gist options
  • Save jonbish/5628807 to your computer and use it in GitHub Desktop.
Save jonbish/5628807 to your computer and use it in GitHub Desktop.
<?php
// Modified from http://wordpress.stackexchange.com/a/5404
function my_search_where($where){
global $wpdb;
if (isset($_GET['all-search']))
$where .= " AND (t.name LIKE '%".like_escape($_GET['all-search'] )."%' OR post_title LIKE '%".like_escape($_GET['all-search'] )."%' OR post_content LIKE '%".like_escape($_GET['all-search'] )."%' OR $wpdb->postmeta.meta_value LIKE '%".like_escape($_GET['all-search'] )."%')";
return $where;
}
function my_search_join($join){
global $wpdb;
if (isset($_GET['all-search']))
$join .= "LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts.ID} = {$wpdb->postmeta.post_id}) LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id";
return $join;
}
function my_search_groupby($groupby){
global $wpdb;
$groupby_id = "{$wpdb->posts}.ID";
if(!isset($_GET['all-search']) || strpos($groupby, $groupby_id) !== false) return $groupby;
if(!strlen(trim($groupby))) return $groupby_id;
return $groupby.", ".$groupby_id;
}
add_filter('posts_where','my_search_where');
add_filter('posts_join', 'my_search_join');
add_filter('posts_groupby', 'my_search_groupby');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment