Title: WP_User_Query Search Forms and Retrieving Custom Fields
Last modified: August 21, 2016

---

# WP_User_Query Search Forms and Retrieving Custom Fields

 *  [raybeam](https://wordpress.org/support/users/raybeam/)
 * (@raybeam)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/wp_user_query-search-forms-and-retrieving-custom-fields/)
 * Hi,
 * I am trying to build a search form for a site I am building that uses the WP_User_Query
   to return results about registered users. I have created 5 new roles and I have
   also created numerous profile fields such as phone number, company name, city,
   etc. What I want to be able to do is create a search form that can filter my 
   users based on those custom fields. I currently have a search form that can find
   all users regardless of their roles. However, if I search for company name, or
   even first name, I get no results.
 * Here is the code I have used so far, which I got from a tutorial on smashing 
   magazine.com:
 *     ```
       <?php
       /*
       Plugin Name: Simple User Listing
       Plugin URI:
       Description: Create a shortcode to list WordPress users
       Author: Ryan
       Version: 1.0
       Author URI: http://www.rayanzenner.com
       */
   
       function sul_user_listing($atts, $content = null) {
       	global $wpdb;
   
       	extract(shortcode_atts(array(
       		"role" => '',
       		"number" => 3,
       		), $atts));
   
       	$role = sanitize_text_field($role);
       	$number = sanitize_text_field($number); 
   
       	ob_start();
   
       	$search = (isset($_GET['as'])) ? sanitize_text_field($_GET['as']) : false;
   
       	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
   
       	$offset = ($page - 1) * $number;
   
       	if($search) {
       	$args = array(
       		'role' => '',
       		'search' => '*' . $search . '*'
       	);
       	$my_users = new WP_User_Query($args);
       	}
       	else {
       	$my_users = new WP_User_Query(
       		array(
       			'role' => '',
       			'offset' => $offset,
       			'number' => $number,
       			'meta_key' => 'company_name',
       			'orderby' => 'meta_value',
       			'order' => 'ASC'
       		));
       	}
       	$total_authors = $my_users->total_users;
   
       	$total_pages = intval(ceil($total_authors / $number)) + 1;
   
       	$authors = $my_users->get_results();
   
               $userid = get_current_user_id();
       	// Create a function to display post count for custom post types
           function count_user_posts_by_type($userid, $post_type = 'post', $post_type_2 = 'projects_posts', $post_type_3 = 'commercial_posts', $post_type_4 = 'removals_posts', $post_type_5 = 'perishables_posts', $post_type_6 = 'exhibitions_posts', $post_status = 'publish') {
   
       	    global $wpdb;
       	    $query = "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = $userid AND (post_type = '$post_type' OR post_type='$post_type_2' OR post_type='$post_type_3' OR post_type='$post_type_4' OR post_type='$post_type_5' OR post_type='$post_type_6') AND post_status = '$post_status'";
       	    $count = $wpdb->get_var($query);
       	    return apply_filters('get_usernumposts', $count, $userid);
   
       	} 
   
       ?>
       	<div class="author-search">
       		<h2>Search authors by name</h2>
       		<form method="get" id="sul-searchform" action="<?php the_permalink(); ?>" role="form">
       			<label for="as" class="assistive-text">Search</label>
       			<input type="text" class="form-control" name="as" id="sul-s" placeholder="Search Members">
       			<input type="submit" class="grey-btn" name="submit" id="sul-searchsubmit" value="Search Members">
       		</form>
       	</div><!-- author-search -->
   
       <?php if(!empty($authors)) { ?>
       	<div class="table-responsive">
       	<table class="table table-striped">
       		<thead class="aio-members-directory">
       			<tr>
       				<th>Logo</th>
       				<th>Company Name</th>
       				<th>City</th>
       				<th>Country</th>
       			</tr>
       		</thead>
       		<tbody>
       	<?php
       	foreach($authors as $author) {
       		$author_info = get_userdata($author->ID);
       		?>
       		<tr>
       			<td class="member-logo-image">
       				<img class="member-profile-img small-image-bg" src="<?php echo $author_info->image; ?>" alt="User image" />
       			</td>
       			<td>
       			<p><a>ID); ?>"><?php echo $author_info->company_name; ?></a></p>
       			<p><?php echo count_user_posts_by_type($author->ID); ?> posts</p>
       			</td>
       			<td>
       			<p><?php echo $author_info->user_city; ?></p>
       			</td>
       			<td>
       				<?php echo $author_info->user_country; ?>
       			</td>
       			<?php }  ?>
       		</tr>
       		</tbody>
       	</table><!-- .author-list -->
       	</div><!-- . table-responsive -->
       ```
   
 * Quite a lot code here and thats not all of it. As you can see, I can retrieve
   the user meta data after the search has been performed. I can even order the 
   default list by custom profile fields, as you will notice. However, if I type
   in a search term such as ‘Bangkok’ for city, nothing is displayed.
 * I know I am probably just doing something fundamentally wrong, but my PHP skills
   are not advanced enough to figure it out.
 * How can I get these custom fields form a search query? There must be a way.
 * Any help greatly appreciated.

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [11 years, 11 months ago](https://wordpress.org/support/topic/wp_user_query-search-forms-and-retrieving-custom-fields/#post-4929130)
 * Sorry for the slow response, I’ve been away.
 * You can query using the ‘meta_query’ argument. See [Class_Reference/WP_User_Query#Custom_Field_Parameters](http://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters).
   One typically knows which field they are searching for, but in your case this
   is not easily discerned unless you needlessly complicate the search form. You
   should be OK by placing the supplied search term in all possible fields, each
   linked together with the ‘OR’ relation.
 * The relation argument is not that well documented, the example for multiple custom
   fields illustrates the correct usage. By using ‘OR’, any fields with no match
   to the search term should be essentially ignored.

Viewing 1 replies (of 1 total)

The topic ‘WP_User_Query Search Forms and Retrieving Custom Fields’ is closed to
new replies.

## Tags

 * [meta-data](https://wordpress.org/support/topic-tag/meta-data/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [wp_user_query](https://wordpress.org/support/topic-tag/wp_user_query/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [11 years, 11 months ago](https://wordpress.org/support/topic/wp_user_query-search-forms-and-retrieving-custom-fields/#post-4929130)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
