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.

Author: Daniel Bachhuber

Proud father x2. Principal, Hand Built. Maintainer, WP-CLI.

12 thoughts on “Include posts by matching authors in your search results”

  1. 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?

      1. 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.

  2. 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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s