Created
          March 8, 2013 02:30 
        
      - 
      
 - 
        
Save hissy/5113780 to your computer and use it in GitHub Desktop.  
    [WordPress] include author's name in query search
  
        
  
    
      This file contains hidden or 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 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