Post as someone else

If you’re logged into WordPress as an administrator, you can easily change the post author of a post to someone else. So what you’ll want to do is set up different user accounts for your different authors and then set yourself up as an administrator. Then when you go to publish a post using your … Read more

How to add user meta for all users

yes, you would have to loop through all users so that you could then update that user_meta field: // Create the WP_User_Query object $wp_user_query = new WP_User_Query(array(‘role’ => ‘Subscriber’)); // Get the results $users = $wp_user_query->get_results(); // Check for results if (!empty($users)) { // loop trough each author foreach ($users as $user) { // add … Read more

Current user in plugin returns NULL

Wait for the action plugins_loaded before you create the class instance. The pluggable functions are loaded at this time. From wp-settings.php: /** * Fires once activated plugins have loaded. * * Pluggable functions are also available at this point in the loading order. * * @since 1.5.0 */ do_action( ‘plugins_loaded’ ); I would even wait … Read more

Get users order by meta key with limit

You can use get_users() with meta_key and order by meta value, descending. Example: $user_args = array( ‘meta_key’ => ‘points’, ‘number’ => 10, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘DESC’ ); $top_users = get_users($user_args); NOTE: Support for meta_value_num is available only from version 4.2

How to query users who have empty first_name?

It looks like you’re searching for the first_name meta keys with empty string meta values: $options = array( ‘meta_query’ => array( array( ‘key’ => ‘first_name’, ‘value’ => ”, ‘compare’ => ‘=’, ), ) ); $users = get_users( $options ); that generates this kind of SQL query: SELECT wp_users.* FROM wp_users INNER JOIN wp_usermeta ON ( … Read more

$user_id vs. is_user_logged_in()

Well it wouldn’t always work unless you global $user_id. is_user_logged_in will however work without that extra line of code. The speed improvement is most likely so small it’s less than the speed improvement between single and double quotes and not even worth thinking about. Also $user_id variable may disappear in a new version and would … Read more

trying to list users & display first – last name

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); $lastName = get_user_meta($user->ID, ‘last_name’, … Read more