WP Search Form Query: Add Author's posts into search query

As the title suggests, I'm looking for WordPress advice relative to leveraging the standard searchform.php functionality to additionally query the author field when performing searches. I've tried various different means to do this without any success, and so I'm coming to quite the headache. Below are the template files used:

searchform.php

?php
/**
 * Template for displaying search forms in Twenty Seventeen
 *
 * @package WordPress
 * @subpackage Twenty_Seventeen
 * @since 1.0
 * @version 1.0
 */

?

?php $unique_id = esc_attr( uniqid( 'search-form-' ) ); ?

div class=global-search
  form role=search method=get class=search-form action=?php echo esc_url( home_url( '/' ) ); ?
    label for=?php echo $unique_id; ?
      span class=screen-reader-text sr-only?php echo _x( 'Search for:', 'label', 'sdwp' ); ?/span
    /label
    input type=search id=?php echo $unique_id; ? class=search-field value=?php echo get_search_query(); ? name=s /
    button type=submit class=search-btni class=fas fa-search/ispan class=screen-reader-text sr-only?php echo _x( 'Search', 'submit button', 'sdwp' ); ?/span/button
  /form
/div

functions.php - the bit of code I've written up after much reiteration

function add_author_to_query( $query ) {

  //do not run on admin side
  if( is_admin() ) return;

  if ( $query-is_main_query()  $query-is_search() ) {

    $submitted_data = get_search_query();

    // Get all users who may match the submitted data
    $users = new WP_User_Query( array(
      'search' = $submitted_data,
      'search_columns' = array( 'user_login', 'user_nicename')
    ) );
    $authors = $users-get_results();

    foreach($authors as $author) {
      $author_ids[] = $author-ID;
    }

    if( ! empty( $authors ) ) {
        $query-set('post_type', array( 'post', 'article', 'page', 'bio', 'author', 'article_type'));
        $query-set( 'author__in', $author_ids );
        // $query-set( 'author__in', $authors );
    }
}
add_action( 'pre_get_posts', 'add_author_to_query' );

Topic query customization Wordpress search

Category Web


For anyone else that stumbles upon this, @admcfajn posted a comment to my question which solved my problem. Below is the code block found in the commented link, attributed to "danielbachhuber" github user:

/**
 * Include posts from authors in the search results where
 * either their display name or user login matches the query string
 *
 */
function db_filter_authors_search( $posts_search ) {

    // Don't modify the query at all if we're not on the search template or if the LIKE is empty
    if ( !is_search() || empty( $posts_search ) ) return $posts_search;

    global $wpdb;

    // Get all of the users of the blog and see if the search query matches either the display name or the user login
    add_filter( 'pre_user_query', 'db_filter_user_query' );

    $search = sanitize_text_field( get_query_var( 's' ) );

    $args = array(
        'count_total' => false,
        'search' => sprintf( '*%s*', $search ),
        'search_fields' => array(
            'display_name',
            'user_login',
        ),
        'fields' => 'ID',
    );

    $matching_users = get_users( $args );

    remove_filter( 'pre_user_query', 'db_filter_user_query' );

    // Don't modify the query if there aren't any matching users
    if ( empty( $matching_users ) ) return $posts_search;

    // Take a slightly different approach than core where we want all of the posts from these authors
    $posts_search = str_replace( ')))', ")) OR ( {$wpdb->posts}.post_author IN (" . implode( ',', array_map( 'absint', $matching_users ) ) . ")))", $posts_search );
    return $posts_search;
}
add_filter( 'posts_search', 'db_filter_authors_search' );

/**
 * Modify get_users() to search display_name instead of user_nicename. Helper function used inside of db_filter_authors_search()
 */
function db_filter_user_query( &$user_query ) {

    if ( is_object( $user_query ) )
        $user_query->query_where = str_replace( "user_nicename LIKE", "display_name LIKE", $user_query->query_where );
    return $user_query;
}

** minor adjustments have been made by me, in the form of spacing and consolidating lines, to make the code more legible.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.