How to Filter Search Results by Users in WordPress

Home » Snippets » How to Filter Search Results by Users in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking for a way to enhance the search functionality on your WordPress website? The default search feature in WordPress is quite powerful, but it may not always provide the level of precision and specificity that you desire. One particular aspect where the default search falls short is filtering results by users. Whether you run a membership site, an online directory, or an e-commerce platform, allowing users to filter search results by specific users can greatly improve the user experience. In this article, we will explore different methods and plugins that can help you implement a user-filtered search on your WordPress website.

					function wpturbo_filter_search_by_users( $query ) {
    if ( is_admin() && $query->is_search ) {
        $user_query = new WP_User_Query( array(
            'search' => $query->query_vars['s'],
            'fields' => 'ID',
        ) );
        if ( ! empty( $user_query->results ) ) {
            $query->set( 'author__in', $user_query->results );
        } else {
            $query->set( 'author', -1 ); // Display no results for invalid user search
        }
    }
    return $query;
}
add_filter( 'pre_get_posts', 'wpturbo_filter_search_by_users' );
				

The code snippet provided demonstrates how to implement a filter for search queries in WordPress, specifically to filter search results by users.

The first step is to create a function called wpturbo_filter_search_by_users that takes in the $query parameter. Inside the function, the code checks if the query is performed in the admin area (is_admin()) and if it is a search query ($query->is_search).

If both conditions are met, the code proceeds to create a new instance of the WP_User_Query class. This class is used to query users based on specific criteria. In this case, we want to search for users that match the search term provided in the query ($query->query_vars['s']) and retrieve the user IDs only (fields set to 'ID').

Next, the code checks if there are any results from the user query (! empty( $user_query->results )). If there are, it means that there are user IDs that match the search term. In this case, the code sets the author__in parameter of the main search query ($query->set( 'author__in', $user_query->results )) to include only posts authored by those users.

On the other hand, if there are no results from the user query, it means that there are no users that match the search term. In this case, the code sets the author parameter of the main search query ($query->set( 'author', -1 )) to a value of -1. This effectively tells WordPress to display no results for invalid user searches.

Finally, the function returns the modified $query object.

To ensure that the wpturbo_filter_search_by_users function is executed at the appropriate time, it is hooked into the pre_get_posts action using the add_filter function.

Overall, this code snippet provides an efficient way to filter search results in WordPress based on users, helping to refine the search experience and provide more accurate and relevant results for the users on the website.

Register an account to save your snippets or go Pro to get more features.