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 want to search posts publish by particular author. For this purpose I added a filter in functions.php:

function wpse_29570_where_filter($where){
        global $wpdb;
        if( is_search() ) {
            $search= get_query_var('s');
            $query=$wpdb->prepare("SELECT $wpdb->users.ID  FROM $wpdb->users WHERE  $wpdb->users.display_name LIKE '%%%s%%' ", $search );
            $authorID= $wpdb->get_var( $query );//die;

            if($authorID){
                $where = "  AND  ( atl_posts.post_author = {$authorID} ) ";
            }
            //echo $where;
         }
         return $where;
    }

    add_filter('posts_where','wpse_29570_where_filter');

It I echo the where condition it gives

AND ( atl_posts.post_author = 1 ) 

but when I search by typing admin it shows a blank result.

Please help me to make it work.
Can anyone tell me a better way to do it?

share|improve this question
1  
Two notes: 1) There's like_query() to wrap like statements in SQL queries - you need to use that. 2) You will maybe want to intercept on posts_clauses hook and _append your custom string to $where .= - note the dot. –  kaiser Mar 20 '13 at 9:51
    
@Kaiser i have change it by appending new condition but it is still blank response. –  Hemc Mar 20 '13 at 11:39
    
Have you checked if the $authorID brings up the correct ID? –  kaiser Mar 20 '13 at 12:01
    
yeah i checked it it brings up the right id. Please have a look to my issue here t9l.com/sane_voice/?s=making and when s=admin it filter posts of recent posts but the main post results are blank. –  Hemc Mar 20 '13 at 12:05
add comment

1 Answer

This line is likely the issue -

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

You are replacing the contents of $where, not changing it. You have to use . before = -

$where.= "  AND  ( atl_posts.post_author = {$authorID} ) ";
share|improve this answer
    
thanks for your response, i think i miss something else also. can you please have a look at t9l.com/sane_voice/?s=making when s=admin or s= hemc it is returning blank response. –  Hemc Mar 20 '13 at 10:28
    
Well I notice that you are checking display_name. By default, I believe this is set to the username, but if you have edited it at all, then your query would not find an ID for user admin. Please check in your admin area under 'Users' and check the display name for the 'admin' account. –  David Gard Mar 20 '13 at 11:16
    
Also, perhaps it would be better to check for both display_name and username in your code, giving the user the choice of entering either? Don't forget that display_name does not have to be unique, where as username does. –  David Gard Mar 20 '13 at 11:17
    
query found id for admin i.e 1. i have check it by echo the where condition. –  Hemc Mar 20 '13 at 11:31
    
Ok, so it's finding the ID for admin then. Just out of interest, what is atl_posts? Have you changed your table prefix from wp_ to atl_? –  David Gard Mar 20 '13 at 11:35
show 3 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.