Possible to search by author name with default WordPress search function?

I realize improving WordPress’s search function is a huge can of worms, but the ONLY additional thing I need it to do is to show results when I search for an author’s name.

For example, if we have an author named Katie Johnson, and I search for Katie, I get results where her name is listed in the CONTENT, but not results that she POSTED.

This seems like a simple problem, and yet I haven’t yet found a solution to it.

Thanks.

3 s
3

Maybe you can try adding your condition directly in the query string, using something like this

function wpse_29570_where_filter($where){
        global $wpdb;
        if( is_search() ) {
            $search= get_query_var('s');
            $query=$wpdb->prepare("SELECT user_id  FROM $wpdb->usermeta WHERE ( meta_key='first_name' AND meta_value LIKE '%%%s%%' ) or ( meta_key='last_name' AND meta_value LIKE '%%%s%%' )", $search ,$search);
            $authorID= $wpdb->get_var( $query );

            if($authorID){
                $where = "  AND  ( wp_posts.post_author = {$authorID} ) ";
            }

         }
         return $where;
    }

    add_filter('posts_where','wpse_29570_where_filter');

Leave a Comment