Capabilities Vs User Meta

There is no one answer, because both have pros and cons depending on what you want to store and why. A (probably non-exhaustive) list of differences to consider for a choice: Capabilities are designed to check if a user can do something or not. user_can and current_user_can are there to help you to check user … Read more

Return all users with a specific meta key

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 its empty!). … Read more

get_user_meta() to Return User Meta Only for Current Blog in Multi Site

WordPress distinguishes usermeta keys between sites by using the database prefix for each site. For example, instead of using the favorite_posts key, you’d use the meta key wp_23_favorite_posts. To get the prefix, you can use $wpdb->get_blog_prefix(). But wait, there’s actually a whole API dedicated to this. Rather than using *_user_meta(), use *_user_option(). These are internally … Read more

wp_update_user doesn’t update and update_user_meta does

wp_update_user & metadata wp_update_user updates records in the *_users table. It isn’t meant to update custom metadata in the *_usermeta table. Hence your “problem” is actually expected behavior. The $userdata argument passed to wp_update_user can contain the following fields: ID, user_pass, user_login, user_nicename, user_url, user_email, display_name, nickname, first_name, last_name, description, rich_editing, user_registered, role, show_admin_bar_front Further … Read more

Query users by custom taxonomy and user role

Apparently there is no core implementation of ‘tax_query’ in WP_User_Query yet. Check the ticket here for more info –> https://core.trac.wordpress.org/ticket/31383 Nevertheless there is an alternative way using get_objects_in_term $taxonomy = ‘shop-category’; $users = get_objects_in_term( $term_id, $taxonomy ); if(!empty($users)){ // WP_User_Query arguments $args = array ( ‘role’ => ‘shop_manager’, ‘order’ => ‘DESC’, ‘orderby’ => ‘user_registered’, ‘include’ … Read more

Check if user is admin by user ID

You worded it as you want to check if user has administrator role. It is considered more proper to check for capability, relevant to specific action. In practice this is usually expressed as current_user_can( ‘manage_options’ ) for current user. For arbitrary user it would be user_can( $user_id, ‘manage_options’ ). Of course this is capability that … Read more

Automatically delete inactive users after 2 months

Your query is wrong because your third argument to TIMESTAMPDIFF is incorrect. You should be using meta_value instead of SELECT meta_value. SELECT user_id FROM wp_usermeta WHERE meta_key = ‘wp_wp_kc_last_active_time’ AND TIMESTAMPDIFF( second, now(), TIMESTAMP(meta_value) ) > 5184000; Try that and see if the results start looking correct. I just checked over the mySQL Date Function … Read more

wp_update_user not updating

Im using remove_role and then add_role to upgrade a user from one role to another. Here is a function that checks all user within the user role subscriber and upgrade them to editor every hour. /** * Add a cron job that will update * Subscribers to editors. Runs hourly */ add_action(‘check_user_role’, ‘upgrade_user’); function run_check_user_role() … Read more

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