How can i filter wordpress users by custom feild?

I'm displaying users (authors) on a page. how can I add a filter based on user custom feilds?

    ?php
                // THE USER QUERY ARGS
                $args = [
                    'role'   = 'author',
                    'number' = -1,
                ];
                // THE USER QUERY
                $user_query = new WP_User_Query($args);

                // THE TOTAL SUBSCRIBERS NUMBER
                echo 'h3 class=font-weight-boldTotal Listed Shops: ' . $user_query-get_total() . '/h3';
                echo 'hr';

                // THE USER LOOP
                if (!empty($user_query-get_results())) {
                    foreach ($user_query-get_results() as $user) {
                        // ACF PREFIX  FIELD DATA FROM USER PROFILE
                        $location = get_user_meta($user-ID, 'yourprefix_location', true);
                        $city = get_user_meta($user-ID, 'yourprefix_city', true);
                        $images = get_user_meta($user-ID, 'blog_group', true);
                        $archive_url = get_author_posts_url($user-ID);
                        echo 'div class=card bg-light p-4';
                        // USER DATA DISPLAY
                        echo 'h1a href=' . $archive_url . ' title=' . __('View all posts by ', 'pippin') . $user-display_name . '' . $user-display_name . '/a/h1';
                        // ACF DATA DISPLAY
                    
                        echo 'pADDITIONAL INFO:/p';
                        echo 'pCity: span class=text-danger' . $city . '/span/p';
                        echo 'pLocation: span class=text-danger' . $location . '/span/p';
                        echo 'h6e-mail: span class=text-danger' . $user-user_email . '/span/h6';

                        echo '/div';
                        echo 'hr';
                    }
                }
                ?

Topic list filters users Wordpress

Category Web


You can make use of the meta_key and meta_value query arguments.

$args = [
    'role'   => 'author',
    'number' => -1,
    'meta_key' => 'yourprefix_location',
    'meta_value' => 'somevalue' 
];

Source: https://developer.wordpress.org/reference/classes/wp_user_query/#custom-field-parameters

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.