Support » Themes and Templates » Search users in a blog

  • saddie.dean


    Can anyone help me out.
    I am making a blog in which there will be multiple users(logged in).
    I have a custom search on the homepage like
    I am using a plugin called wp-members which allows me to make a custom registration page, custom fields for user registration. Ill add description for a user in wp-members fields like doctor or dentist or patient etc.
    How do I query relative search items?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Chad Butler


    Extra user fields in WP-Members are stored in the usermeta table. You’ll probably need to do that with the $wpdb class and a query of the usermeta table for the appropriate user meta field.



    Thanks for the answer Chad I know that it stores data in usermeta field. It would be something like this:
    $wpdb->get_results(SQL querying usermeta field) and then a foreach will work for it. I am confused what query should work and how would i link that query with my search. I was searching a plugin for user search with 2 or 3 dropdown selections but couldn’t find it 🙁 I am a newbie for sql and WordPress. Suppose I will store speciality and location for each user, how would I display those users whose speciality and locations would be selected from the dropdown search like in ?

    Chad Butler


    Since what you have described (limiting to 2 or 3 set dropdown selections) is more of a filter than a search (which would be more ‘freeform’), I would be more inclined to use built in functions rather than write a SQL query (although you are on the right track with that as well).

    You could get all users using the function get_users, then loop through that list. For example, if you wanted to show a list of doctors that spoke Spanish (assuming you had a meta key of “language” and “Spanish” was one of the possible values:

    $users = get_users();
    foreach ($users as $user) {
    	$last_name = get_user_meta( $user->ID, 'last_name', true );
    	$spanish = get_user_meta( $user->ID, 'language', true );
    	if( $spanish == 'Spanish' ) {
    		echo 'Dr. ' . $last_name . '<br />';

    This would get all users, then loop through the resulting array, testing for the meta value of “Spanish” using get_user_meta and if it was there, then displaying accordingly.

    You could build on this concept significantly, such as allowing for additional conditions, and you’ll need to collect the parameters from the search form.

    Also, get_users allows you to retrieve the meta values you ask for as part of the array so you could eliminate the get_user_meta step and have something like $user->last_name and $user->language, but that gets a little more complicated because you need to understand how to pass the arguments to get_users() as an array.

    You could potentially use or combine this with get_userdata as well.

    I think that’s probably easier in the long run than using SQL queries directly. Hope that helps.



    Thanks Chad. I’ll soon try this and let you know.
    New year greetings in advance 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search users in a blog’ is closed to new replies.