Include posts by matching authors in your search results

Out of the box, WordPress’ search isn’t that great. It only returns posts based on a LIKE query against the post title or post content. Often, you’ll want it to match against other data associated with your post, possibly including the author, tags, post meta fields, etc. These queries can get complex to perform on the fly, however.

The following code snippet allows you to include posts by matching authors in your search results. We’re modifying any search queries to also include all posts whose author display name or user login matches the query. You can change this to be the author’s first name, last name, or other fields.

Updated May 17, 2012: Improved the user search so we don’t query for all users and search with PHP; instead, search against the users table.


scormeny May 7, 2012 Reply

Daniel thanks so much for this elegant code snippet. I’m sorry to say I’m such a n00b and don’t know enough about where all of these things might go in WordPress. Into what file would you paste this code?

Daniel Bachhuber May 7, 2012 Reply

Feel free to simply drop it in your theme’s functions.php file 🙂

scormeny May 7, 2012 Reply

Great suggestion, thanks! I thought about it a little harder on my own before you wrote back, and ended up making it a custom plugin for my site, called “Include Author in Search”. All of your original text from the code snippet is included so no credit-stealing! It’s working like a charm on my test site, I’ll send you the final link once it’s live.

scormeny May 10, 2012 Reply

Hey, Daniel, troubleshooting question! When I am logged into my site as the administrator, this plugin is working fine for me as a user. But when I log out, it does not work any longer, and it is not working for other people who are not logged in.

Any thoughts?

Thank you!

Daniel Bachhuber May 11, 2012 Reply

Mind opening a new forum thread? It’s my preferred way of doing support. Thanks!

scormeny May 28, 2012 Reply

Thanks so much, Daniel, that seems to have it fixed. Take care!

Dwain Maralack September 18, 2012 Reply

This solution helped me a lot. Thank you Daniel!

Motaz Elshazly March 26, 2014 Reply

Works beautifully, thank you!

mayank saxena April 7, 2014 Reply

it worked well.. thanks for the snippet..

Bowman Kelley October 9, 2015 Reply

Awesome Implementation! Thanks!

Luis Martinez (@CodeLuisCode) February 22, 2016 Reply

Just what I was looking for. Thank you so much!

Banago July 11, 2016 Reply

This is a great solution. Good thing I googled it before implementing it. It’s exactly what I need. Thanks so much Daniel.

reeta October 8, 2018 Reply

Thank you daniel for this code. i added in function.php and it is working fine for my website.

Daniel October 9, 2018 Reply

Thanks Daniel, it was very helpful.

Leave a Reply