WordPress.org

Support

Support » How-To and Troubleshooting » get_users meta_query with multiple custom fields

get_users meta_query with multiple custom fields

  • Hello
    I am working on one filed user search.
    Basicly I need one field (like default WordPress search) that will search in users for that search string.

    I have made a few custom fields:
    city (string)
    country (drop-down list of pre-defined countries)
    user_description (I have removed default”bio” textarea and made new one.)
    telephone
    mobile phone

    So I would like to search in these custom fields for provided search string.
    I was working on meta_query but either I get a list of all users or none so ever.

    I was working on this code:

    $search_word = "eng";
        $args = array(
    		'role' => 'contributor',
            'meta_query'   => array(
                    'relation' => 'OR',
    
                array(
                    'key' => 'country',
                    'value' => $search_word,
                    'compare' => 'LIKE'
                ),
                array(
                    'key' => 'city',
                    'value' =>  $search_word,
                    'compare' => 'LIKE'
                )
    
           )
        );
    $users = get_users($args);
    foreach($users as $user) {
    if ( $user->city ) { echo $user->city; };
    }

    So I need to search in multiple custom fields where in ANY of these fields has a value LIKE the search string.

    Can someone give me a hint what I did wrong?

    And another question:
    Can I “order” the results by custom field or only on default fields?

    Thank you all for your help.

    Regards
    Matic

  • The topic ‘get_users meta_query with multiple custom fields’ is closed to new replies.