QuestionsCategory: Technical QuestionHow to include post authors in search results?
Steve Parker
asked 5 months ago
searching for posts by authors produces no results. How do i include authors in the search qeury?

1 Answers
tech Staff
answered 5 months ago
Hi, If you prefer to include the code directly in your theme files instead of using a plugin, you can follow these steps:
1. Open your WordPress dashboard and go to “Appearance” -> “Editor” or access the theme files via FTP.
2. Locate the functions.php file in your theme’s directory.
3. Edit the functions.php file and add the following code at the end:

// Include authors in search query
function include_authors_in_search_query($query) {
    if (is_admin() || !$query->is_main_query()) {

    if ($query->is_search) {
        $author_ids = array();
        $authors = get_users(array('fields' => 'ID'));
        foreach ($authors as $author_id) {
            $author_posts = new WP_Query(array(
                'post_type' => 'post',
                'author' => $author_id,
                'posts_per_page' => -1,
                'fields' => 'ids',
            if ($author_posts->have_posts()) {
                $author_ids[] = $author_id;
        $query->set('author__in', $author_ids);
add_action('pre_get_posts', 'include_authors_in_search_query');

replied 5 months ago

This code adds a hook to the pre_get_posts action, which allows us to modify the search query before it is executed. It retrieves all the authors’ IDs and includes them in the search query using the author__in parameter.

Save the changes to the functions.php file and upload it to your theme directory if you made the edits locally.
After adding this code, searching for an author’s name should display posts written by that author in the search results on your WordPress site.

Your Answer