Skip to content

Instantly share code, notes, and snippets.

@hissy
Created March 8, 2013 02:30
Show Gist options
  • Save hissy/5113780 to your computer and use it in GitHub Desktop.
Save hissy/5113780 to your computer and use it in GitHub Desktop.
[WordPress] include author's name in query search
<?php
function custom_search_join( $join ) {
global $wpdb;
if( is_search() ) {
$join .= " INNER JOIN $wpdb->users ON "
. "$wpdb->posts.post_author = $wpdb->users.ID ";
}
return $join;
}
function custom_search_where( $where, $query ) {
global $wpdb;
if ( $query->is_search() ) {
$where = preg_replace(
"/\(\s*$wpdb->posts\.post_content\s+LIKE\s*(\'[^\']+\')\s*\)/",
"($wpdb->posts.post_content LIKE $1) OR ($wpdb->users.display_name LIKE $1)",
$where
);
}
return $where;
}
add_filter( 'posts_join', 'custom_search_join' );
add_filter( 'posts_where', 'custom_search_where', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment