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: https://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.
- The topic ‘WP_User_Query Search Forms and Retrieving Custom Fields’ is closed to new replies.