Echo the number of users using WP_User_Query?

We have the public properties: WP_Query->found_posts WP_Comment_Query->found_comments WP_Network_Query->found_networks WP_Site_Query->found_sites but then comes this private property (that’s also made public via magic getter): WP_User_Query::$total_users but not found_users as expected, so the confusion is natural 🙂 Look into the public get_total() method and the count_total bool attribute (true by default) in the docs.

Get users in query and limit user output to five in random order

Yes possible, just tell WordPress to use RAND() SQL sorting when passing rand in orderby parameter: add_action( “pre_user_query”, function( $query ) { if( “rand” == $query->query_vars[“orderby”] ) { $query->query_orderby = str_replace( “user_login”, “RAND()”, $query->query_orderby ); } }); Now you can use: $users = get_users( array( ‘meta_key’ => ‘last_name’, ‘orderby’ => ‘rand’, ‘number’ => 3 // … Read more

WP_User_Query Custom Field meta_query with Date clause

You can set the type of the data in your meta_query. Unfortunately, you can only use the comparison BETWEEN if your date is in the format YYYYMMDD. So to see the users for one year, you would have to set your metaquery like this: $usersByCompanyArgs = new WP_User_Query( array( ‘fields’ => ‘all_with_meta’, ‘orderby’ => ‘bhaa_runner_dateofrenewal’, … Read more

Sort WP_User_Query by meta_key value with pre_user_query

As written, this is unlikely to do anything. If you add a couple of var_dumps like this: function sort_by_member_number( $vars ) { var_dump($vars); if ( isset( $vars->query_vars[‘orderby’] ) && ‘member-number’ == $vars->query_vars[‘orderby’] ) { $vars = array_merge( $vars->query_vars, array( ‘meta_key’ => ‘arcc_member_number’ ) ); } var_dump($vars); return $vars; } add_filter( ‘pre_user_query’, ‘sort_by_member_number’ ); You will … Read more

Want to add my custom prepare query but add_filter doesn’t run

The WP_User_Query allows meta_query searches exactly like the other WP_*_Query classes. Example here: global $wpdb; $author_search = new WP_User_Query( array( ‘role’ => ‘subscriber’, ‘fields’ => ‘all_with_meta’, // if it’s a digit/int/float, use ‘meta_value_num’ ‘orderby’ => ‘meta_value’, ‘order’ => ‘ASC’, // you could try -1 as well. ‘nuber’ => 99999999, ‘meta_query’ => array( ‘key’ => ‘my_userpoints’, … Read more

User appears twice in a WP_User_Query

Try this for kicks… Solution 1 Note: Does not work with WP_User_Query (?) add_filter(‘posts_distinct’, ‘user_meta_query_distinct’); //your query here… remove_filter(‘posts_distinct’, ‘user_meta_query_distinct’); function user_meta_query_distinct() { return “DISTINCT”; } Solution 2 Can you try adding ‘relation’ => ‘OR’ to your meta_query: $user_query = new WP_User_Query( array( ‘role’ => ‘member’, ‘orderby’ => ‘registered’, ‘order’ => ‘DESC’, ‘meta_query’ => array( … Read more

WP_User_Query users by registered date

You can simply use the date_query parameter on the user registration date: $args = array ( ‘role’ => ‘subscriber’, ‘date_query’ => array( array( ‘after’ => ‘2010-01-13 00:00:00’, ‘inclusive’ => true, ), ), ); $user_query = new WP_User_Query( $args ); This part of the WP_User_Query source code, makes it possible: // Date queries are allowed for … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)