apply filter on email and meta WP_User_Query

时间:2016-10-20 13:21:20

标签: wordpress

I have some custom meta like first_name, last_name and address. I want to make a WP_User_Query to search like keyword like first_name or last_name or address or email or username.

1 个答案:

答案 0 :(得分:0)

You can do that using WP User query, it actually fetches data from the wp_usermeta table so no problem the link you're looking for is : https://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters

You could do :

$args = array(
'meta_query' => array(
    'relation' => 'OR',
        array(
            'key'     => 'first_name',
            'value'   => '$your_variable,
            'compare' => '='
        ),
        array(
            'key'     => 'last_name',
            'value'   => '$your_variable2,
            'compare' => '='
        ),
        array(
            'key'     => 'address',
            'value'   => '$your_variable3,
            'compare' => '='
        ),
)
);
$user_query = new WP_User_Query( $args );

The $your_variables should be what you're looking for like a $_POST['first_name'] that you'd get from a form.

Regarding the email and username. Same idea but with the search parameter from the exact same class : https://codex.wordpress.org/Class_Reference/WP_User_Query#Search_Parameters