How to search for partial match display names of WordPress users

How to search for partial match display names of WordPress users

You can use the WP_USER_QUERY Class to search through the display_name column of WordPress Users in the Database and return those users.

Table of Contents

Why search a partial match?

You might want to search a partial match if you have some sort of User Search field which needs to return results as the user is typing in real time so they can effectively search and select in one field. Read below in case you want to do this as I will provide some code in order to do that on a field in WordPress.

What is the Display Name?

A Display Name in terms of WordPress User accounts is technically a piece of data that sits on the wp_users table in the database and represents an alternative ‘name’ to display on the front end of the site which is no the username, email or first and last name.

In my own experience, the display name has sometimes been the only consistently available and human relatable piece of info to search. In my case the usernames and emails were hard for a human to relate to a specific user so Display Names can be useful that way.

Where else might you want to search a partial match?

You could also replace a search on a display name to another field like username, email, first name etc as per you applications needs. These are also columns on the wp_user table so you could so the below examples on any of these.

All the columns you could search on in the wp_users table

You can also look here in the codex for the table columns for this and all other tables in WordPress.

Code to search a partial match on Users in WordPress

You can use the WP_USER_QUERY Class to search a partial match on the display name (or any other column as above) using the below code.

<?php
    $args = array(
	'search'         => '*searchString*',
	'search_columns' => array( 'display_name' )
    );
    $users = new WP_User_Query( $args );
    
    // Here is a list an array of all the searchable columns on the User Table
    array( 
        'display_name',
        'user_login',
        'user_pass',
        'user_nicename',
        'user_email',
        'user_url',
        'user_registered',
        'user_activation_keyuser_status'
    )

To explain this code a little,

  • The WP_User_Query Class allows you to search Users in a lot of different ways and the specific search related API docs.
  • The ‘search’ key allows you to enter the partial string with which you want to search the display_name column.
  • When you add the * before and after the string you are telling WP_User_Query to search the string in between the *. The search string can be anywhere in the display_name.
    • If you were to have just ‘*searchString‘ you would search for the string only at the end of the display_name.
    • If you were to have just ‘searchString*’ you would search for the string only at the beginning of the display_name.
  • There is also a ‘search_column’ key which tells WP_User_Query which columns to search for the string. You could add more of the available columns. A full list of the available columns as above.

Learn WordPress customization throughly with an online course at Udemy

If you are having difficulty understanding this tutorial and then I suggest you start from scratch with WordPress in general. The best way to learn is with a better all around general understanding of WordPress. Check out some of the courses on Udemy related to WordPress. They cover developer level courses and more non-dev CMS editor type tutorials as well.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *