Tag Info

Hot answers tagged

7

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


6

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


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


4

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

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

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


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

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


3

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

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

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

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

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


3

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_userdatabylogin($login); $now = time(); update_usermeta( $user->ID, 'user_last_login', ...


2

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() ); }


2

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


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

List authors by Total Post Views - If view is custom field name then this functions will return all users in descending order as per their post's TOTAL view count. Just drop in this function in your theme's functions.php file . Call function wpse_61194_top_authors('view') with meta_key name. // Usage - // $list = ...


2

I don't think it's possible to do it with the get_users function. From what the Codex implies, you cannot pass arrays to the role argument. But it should be fairly easy to code your way out of that limitation. Try this: function filter_two_roles($user) { $roles = array('academic','student'); return in_array($user->roles[0], $roles); } $users = ...


2

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


2

when you use get_users() it retrieves an array of users matching the criteria given in $args which means you can simply use PHP's count() function e.g: $users = get_users($args); $number_of_users = count($users);


2

There's the WP_User_Query for that: /** * List all Users * Use as: Template Tag * * @uses WP_User_Query * @return (array) List of user objects */ wpse35713_get_users_list() { $query = new WP_User_Query( array( 'order' => 'ASC' ,'orderby' => 'login' ) ); $users = $query->get_results(); $html = ...


2

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


2

In your original code, you're not passing an operator to meta_compare. Note that get_users() does not define a default operator. Try using '=': $args = array( 'meta_key' => 'custom-usermeta', 'meta_value' => $cat_id, 'meta_compare' => '=' ); $users = get_users( $args ); As a diagnostic, you might make sure that the problem isn't the ...


2

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


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

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



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