WP_User_Query Search Custom Fields
-
I am trying to create a custom search form that will return results based on custom profile fields that I have defined on a clients site.
Basically, I have fields such as ‘company_name’, ‘phone’, ‘user_city’ etc and I want to be able to filter my search results by these fields.
I know how to set up WP_User_Query in the most part, it makes sense to me and seems easy. The problem I have is getting the search form to display results from these fields. My search form only returns results for things like username. I can’t even return results based on first name.
Have a look at my code example, I’ve condensed it for the sake of brevity:
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; // return results based on search term if($search) { $args = array( 'role' => '', 'fields' => 'all_with_meta', 'search' => '*' . $search . '*', 'meta_query' => array( 'key' => 'company_name', 'value' => '', ) ); $my_users = new WP_User_Query($args); } else { // default results when searching all users $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(); // function count_user_posts_by_type() // displays post count for CPTs ?> <div class="author-search"> <h2>Search authors by name</h2><!-- Need to search by company name, country, and city --> <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 href="<?php echo get_author_posts_url($author->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 -->I am able to access this meta data, as you can see in my foreach loop I am accessing $author_info->user_city, which is a custom field. I can output that in my results, but if I search for an existing city, such as Bangkok, nothing is returned.
Any help appreciated.
The topic ‘WP_User_Query Search Custom Fields’ is closed to new replies.