Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

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} ) ";
            }
            echo $where;
         }
         return $where;
    }

    add_filter('posts_where','wpse_29570_where_filter');
share|improve this answer
    
I understand what you're trying to do here, but it unfortunately, is not working for me. This actually unsets the query string and then sets author_name to nothing. I believe the results are EVERYTHING that's been posted. –  zack Sep 27 '11 at 14:41
    
Updated my answer to something that could work better –  Hansy Schmitt Sep 27 '11 at 15:44
1  
I updated to this code, and it doesn't seem to be returning all the results from the user. While it does return SOME results, I'm not sure which ones its picking and which ones are being left out, or WHY. Basically, the results for a search of a user's name and the archive for that user are drastically different. –  zack Sep 27 '11 at 18:22
    
Well this will return for the search term and results for the user first or lastnmae close to the string you are searching for, if you want results ONLY from the author then you will change $where = " {$where} OR ( wp_posts.post_author = {$authorID} ) "; with $where = " AND wp_posts.post_author = {$authorID} "; Or if you want to search other users field you may need to edit the first edit field –  Hansy Schmitt Sep 27 '11 at 19:00
    
the first situation you described is what I want it to do, but unfortunately not what is happening. A search result for "Stephanie" (without quotes) returns only ONE result where the query is the author (but 3 other normal results). when I navigate to that users archive, I see all SEVEN posts by them. –  zack Sep 27 '11 at 20:39
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.