Hot answers tagged

17

You can also do this via a single call to get_users or using a single WP_User_Query by making use of the meta_query argument: global $wpdb; $blog_id = get_current_blog_id(); $user_query = new WP_User_Query( array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => $wpdb->get_blog_prefix( $blog_id ) . ...


12

I managed to solve this by using this function: function get_clients() { $users = array(); $roles = array('subscriber', 'custom_role1', 'custom_role2'); foreach ($roles as $role) : $users_query = new WP_User_Query( array( 'fields' => 'all_with_meta', 'role' => $role, 'orderby' => ...


10

The field user_registered is not a in the *_usermeta table, but the in *_users. Nor do there seem to be any appropriate hooks to alter the SQL statement when using WP_User_Query. It seems you will need to use a direct SQL statement. Another (minor) complication is that the registered date is stored as a date-time. function ...


6

first_name and last_name are stored in the usermeta table. Therefore you have to use get_user_meta() to return these data. Try this code snippet: $users = get_users(array( // blog_id is not required and will be set by WP_User 'role' => 'sm_flagar' )); foreach ($users as $user) { $firstName = get_user_meta($user->ID, 'first_name', true); ...


6

global $wpdb; $min_posts = 5; // Make sure it's int, it's not escaped in the query $author_ids = $wpdb->get_col("SELECT `post_author` FROM (SELECT `post_author`, COUNT(*) AS `count` FROM {$wpdb->posts} WHERE `post_status`='publish' GROUP BY `post_author`) AS `stats` WHERE `count` >= {$min_posts} ORDER BY `count` DESC;"); // Do what ...


5

Have you tried to go with the "Safe usage" alternative given in the commented section? I honestly don't have any experience with wp_get_current_user(), since I never use it, but anyhow, this ought to work: global $current_user; echo 'Username: ' . $current_user->user_login . '<br />'; echo 'User email: ' . $current_user->user_email . '<br ...


5

First get the total number of users: $total_users = count_users(); //var_dump($total_users); //for debugging purpose $total_users = $total_users['total_users']; and the current page: $paged = get_query_var('paged'); Set a variable that will decide how many users to display per page: $number = 20; // ie. 20 users page page Then, in your $args array ...


5

As it mentions in the Codex page for get_users, it works the same as a WP_Query meta_query, see that page for full arguments list and examples. $args = array( 'meta_query' => array( array( 'key' => 'some_key', 'value' => 'foo', 'compare' => '=' ), array( 'key' => ...


5

The Native get_users() function returns an array of user objects and each on holds [ID] => 1 [user_login] => admin [user_pass] => $P$Bxudi6gJMk2GRt2ed3xvZ06c1BPZXi/ [user_nicename] => admin [user_email] => admin@host.com [user_url] => http://localhost/ [user_registered] => 2010-06-29 07:08:55 ...


5

Few months ago, I updated the Codex for get_users() and WP_User_Query, regarding the date_query support on the user's registration date, in WordPress 4.1+. Then I also added a simple example on how to find users that registered during the last 12 hours. But the current question is how to find users that registered today. We could simply use relative date ...


5

Fastforward to WordPress 4.4 - it will support the role__in attribute! It looks like WordPress 4.4 is our lucky version number, because it will support both the role__in and role__not_in attributes of the WP_User_Query class. So to include the subscriber, contributor and author roles, we can simply use: $users = get_users( [ 'role__in' => [ ...


4

I would highly recommend Daniel's much simpler solution over the one currently selected as correct: $user = get_userdata( $user_id ); if ( $user === false ) { //user id does not exist } else { //user id exists }


4

First, you need to store the actual login date, because this is not stored by default. You can use this code to do that(use it in your functions.php) add_action('wp_login','user_last_login', 0, 2); function user_last_login($login, $user) { $user = get_user_by('login',$login); $now = time(); update_usermeta( $user->ID, 'user_last_login', ...


4

You need to set the who parameter in get_users <?php $blogusers = get_users( 'orderby=post_count&who=authors' ); foreach ( $blogusers as $user ) { echo '<li>' . esc_html( $user->display_name ) . '</li>'; } ?> EDIT Seems I was to fast answering. The code in your question and in my answer is the start to what you ...


4

Just in case anyone (just like me) comes across this thread, you can extend this to show custom fields. Add this to your theme's functions.php: // Extend user profile // CUSTOM USER PROFILE FIELDS function my_custom_userfields( $contactmethods ) { // ADD CONTACT CUSTOM FIELDS $contactmethods['contact_phone_office'] = 'Office Phone'; ...


4

Made something myself: Page template file: <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $paged -= 1; $limit = 20; $offset = $paged * $limit; $args = array( 'number' => $limit, 'offset' => $offset, ); // Create the WP_User_Query object global $wp_query; $wp_query = new WP_User_Query($args); // Get the results ...


3

You can... Load the file into the file where you want to display the 'hey username' message: <?php include(TEMPLATEPATH .'/check-user-hello.php'); ?> . Then in that file "check-user-hello.php" You need to put this code <?php if ( is_user_logged_in() ) { global $current_user; get_currentuserinfo(); echo 'Hey ' . $current_user->display_name; ...


3

wp_list_authors(), internally gets posts that are only of type post. See line 294 http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/author-template.php#L273 As you have noted, this answer is great and with some modification can do what you want. function wpse31443_author_has_custom_post_type( $post_author, $post_type ) { global ...


3

WP_User objects have some magic methods which do allow you to access any custom field: foreach ( $agents as $agent ) { var_dump( $agent->bio ); var_dump( $agent->get( 'bio' ) ); } The two are equivalent. More info: http://scribu.net/wordpress/the-magic-of-wp_user.html


3

Put this in your functions.php: function get_users_ordered_by_post_date($args = '') { // Prepare arguments if (is_string($args) && '' !== $args) parse_str($args, $args); $asc = (isset($args['order']) && 'ASC' === strtoupper($args['order'])); unset($args['orderby']); unset($args['order']); // Get ALL users ...


3

Unless you want to retrieve custom data from the database, you will hardly ever need to make use of the WPDB class (or its global object, respectively). Though it is obviously possible to do things that way as well. Just for the sake of completeness, if you had a reason to not use a more abstract function, you'd have to employ JOIN in the select syntax as ...


3

There is no default way in WordPress to do this task, as Pieter Goosen pointed out, exists the argument who for get_users() that returns users that can post, not the users that have posted. However, you can use 'pre_user_query' to add a JOIN SQL clause to only get users that have at least one post. To be honest, when you query users ordering them by post ...


3

You can just call get_user_meta without specifying a key, and it will return all MetaValues for the user. $userdata = get_user_meta( $userID ); You may have to do a mapping for displaytitles of the Metavalue, as they are returned in an array, where the keys are the databasevalues of your metakeys. For example, you may get ...


3

passatgt's answer can be simplified slightly. No need to grab the user object, when it's already present as the second argument: add_action( 'wp_login','prefix_save_user_last_login', 0, 2 ); function prefix_save_user_last_login( $login, $user ) { update_usermeta( $user->ID, 'user_last_login', time() ); }


3

The problem with using array_merge is that you can't use pagination. I really like @Andy Adams's solution, but if you're searching on many roles, using his meta query will result in a very slow query (internally it does a new INNER JOIN for each meta query). My solution is to use a regular expression meta query: <?php global $wpdb; $blog_id = ...


3

The original code is not so bad. It just is missing the meta_key parameter. Example: $theusers = get_users( array( 'role' => 'contributor', 'meta_key' => 'first_name', 'orderby' => 'meta_value', ) );


3

There is a better way to do this as of Wordpress version 3.7. Use the Wordpress property meta_key to select the last name property and then orderby => meta_value with an ascending order. <?php $args = array( 'meta_key' => 'last_name', 'orderby' => 'meta_value', 'order' => 'ASC' ); $user_query = new WP_User_Query( $args ); if ( ! ...


3

You can use the just the meta_key argument, the result will be similar to using the EXISTS sql statement. <?php $users = get_users(array( 'meta_key' => 'your_meta_key', )); Alternatively, you can use an empty string for meta_value (the default) and > for meta_compare. The result is the same (probably because meta_value gets ignored if ...


3

Below is my own answer to the question. I will appreciate any constructive comment. add_shortcode( 'group_members', 'group_members_shortcode_handler' ); function group_members_shortcode_handler ( $atts, $content = null ) { global $wpdb; $querystr = "SELECT * FROM wp_groups_user_group"; $users = $wpdb->get_results($querystr, OBJECT); ...


3

I don't see an "orderby Rand()" parameter for either get_users or WP_User_Query. There is a filter called pre_user_query that could be used but I am not sure I see the benefit of that when shuffle will randomize the array you already have. $args = array( 'fields' => 'all_with_meta', 'exclude' => array(1), ); $users = get_users( $args ); ...



Only top voted, non community-wiki answers of a minimum length are eligible