Tag Info

Hot answers tagged

10

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 ) . ...


8

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' => ...


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; get_currentuserinfo(); echo 'Username: ' . $current_user->user_login . '<br />'; echo 'User email: ' . ...


5

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); ...


5

First get the total number of users: $total_users = count_users(); $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 add the offset if the current page != 0, and the ...


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' => ...


4

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 ...


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

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 ...


4

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 ...


4

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 ...


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'; ...


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

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

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

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 ); ...


3

You don't want to fetch all users if you only need 5. If you want to use WP_User_Query() to fetch 5 users by random, you can try to use the pre_user_query hook to overwrite the orderby part: $args = array( 'role' => 'subscriber', 'number' => 5, ); add_action( 'pre_user_query', 'my_pre_user_query' ); $wp_user_query = new WP_User_Query( ...


3

If you are concerned about efficiency, you might want to use the Transients API to store the query. Storing something that you want to randomize might seem counter-intuitive, but if you store the entire query, you can always randomize and manipulate the resulting array to get the results you want. Here's how to get all the subscribers and store them in a ...


2

Something like this should get you a random user. Not tested. function display_random_user(){ $users = get_users(); $user_info = get_userdata($users[array_rand($users)]->ID); // do stuff with info } PHP: array_rand - Manual


2

By default WordPress allow only order ASC and DESC. However, you can use WP_User_Query and the action pre_user_query to adjust the query (that is passed by reference) just like you want. This is efficient because you get only one user, not all. function my_user_by_rand( $ua ) { // remove the action to run only once remove_action('pre_user_query', ...


2

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); ...


2

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 = ...


2

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 ...


2

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 ( ! ...


2

Sometimes it is more useful to look at the task outside of the WordPress field, but from true programming. You can use custom SQL query to get your users ids and then do what you want with them. Place the code below into functions.php function wpse_177536_get_user_ids(){ global $wpdb; $sql = "SELECT user_id FROM $wpdb->usermeta WHERE ...


2

Your code doesn't work because you're using $bloguser instead of $user. And there's no point in calling get_userdata() since get_users() already returns user objects: foreach ($blogusers as $user) { echo '<tr>'; echo '<td>'. $user->first_name .'</td>'; echo '<td>'. $user->last_name .'</td>'; ...


2

Filter the string 'query' and change the ORDER BY part. See https://gist.github.com/1281778#L160 for an example.



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