It looks like your $user variable contents are not an instance of a WP_User class. You’ve got an error of stdClass, but you need the WP_User. Check what class the $user is the instance.
If you use the get_users() function, you should note one feature of this function described in the documentation:
Return value is an array of IDs, stdClass objects, or WP_User objects, depending on the value of the ‘fields‘ parameter.
- If ‘fields‘ is set to ‘all’ (default), or ‘all_with_meta’, it will return an array of WP_User objects.
- If ‘fields‘ is set to an array of wp_users table fields, it will return an array of stdClass objects with only those fields.
- If ‘fields‘ is set to any individual wp_users table field, an array of IDs will be returned.
Related Posts:
- How to get role of user
- How do I programmatically set default role for new users?
- What do unfiltered_html and unfiltered_upload actually filter?
- Temporary capability for current_user_can()
- How to update role capabilities
- How to loop through each user id?
- Code needed to only show users own posts in a multi-user account
- Ordering users of a specific role by last name
- Restrict Author to pick from media library, but not upload media
- how to add custom user capabilities using add_user_meta or something else?
- Remove Ability for Administrators to Delete Administrators
- Order users by custom user meta
- how to change user roles for users who doesn’t have any. (about 8000 users)
- List total number of users that are authors
- I need to assign a role to visitors/guests
- Auto Delete Users (auto_delete_users)
- New Roles and Capabilities in WordPress
- WordPress Super Admin
- How to get all users with Author role capabilities?
- How to ‘unpublish’ or ‘hide’ posts when user role changes?
- Can’t manage to make translate_user_role() work
- Add Role inherits?
- similar to Editor can create any new user except administrator
- How to check if a role has a specific capability
- Expire a user’s secondary role after X days from it being allocated
- Can a user with admin role get ‘Sorry, you are not allowed to move this item to the Trash’ error when trashing a post?
- Making shortcode of filtered number of comments by user role
- How to stop contributors editing post type but allowing them to edit a custom post type?
- Allow Block Editing By User Role
- WordPress missing user roles on local dev machine. Live site works fine
- Programmatically allow a non-author to edit a post based custom meta field
- Customizer – Allow user editor to change website title and tagline
- Query users which have same dynamically generated roles as the current user
- change role of wordpress users when they edit profile
- Change post status based on user role
- Display User Role Next To Comment
- Pending status by default for a specific role
- Getting a role based on a localized role name
- Allow an editor to change the author
- How do I restrict user access to plugins?
- Admin User Role Unable to See Private Posts
- How to give remove role editing permission from user
- How to have kind of a technical writer role in wp website
- Assigning certain authors to specific editors
- Undo User Role Name Change
- add_role user capability not working
- How to make the Newsletter plugin visible to users with author privileges?
- WP welcome email depending on user role
- Limit a user to have access to only specified pages?
- How can I promote a user to a network administrator?
- read_private_pages capability not working for new role
- Updater user with multiple roles
- Custom user types getting html sanitized in posts
- How to stop displaying the Id without losing the functionality
- Restrict custom post content to specific user
- Hiding custom theme functionality using capabilities
- Use PHP code to create custom user roles. Call it once?
- How can I programattically hide all admin notices for everyone except admin users
- custom plugin not visible to some user roles
- How to exclude subscriber from the list in wordpress
- What Capability is required to let a role RUN code in Edit Theme?
- Create sub-administrator role that can do everything except use or see the code editor
- display user roles in comment form
- Need to block user role from accessing bbPress all together
- Is it proper to build a site supporting blog visitors in WordPress?
- Removing capabilities from cloned admin role
- Retrieve user roles but exclude default roles
- Setting an expiration date for wordpress roles
- Could we know who published a post on WordPress?
- Disabled delete_others_posts if post is from admin
- Auto Change WordPress Roles
- Custom Plugin – Blacklist/Whitelist domain email and change user role – Suggestions
- Block access to plugins.php for custom role
- How to hide Add new Term area for taxonomy terms for specific role (Client)
- How to make WP page accessile only to specific user roles
- Remove add post for user role
- Saving user meta “member_id” based on user role
- show the author’s role along with the name in the single.php
- WordPress – Limit custom/specific user role to only have access to edit the contents of an Advanced Custom Fields Pro Options Page
- TCPDF only exports .pdf when user is administrator
- How to allow suscriber to access specific pages in dashboard?
- Enable plugins for a specific user role
- Switch role on submit button
- How to Enable registration email for a custom user role?
- Multiple user role on registration
- Trying to get new role to have edit permissions for custom content type
- Why are all roles assigned to a user when using wp_insert_user()?
- How can I add the ability for a user to add/edit administrators?
- Custom user role doesn’t show up in admin
- Welcome email specific to user role
- add user role selection to registration form [duplicate]
- Assign specific editor with custom user meta “A” to all authors with custom user meta “A” and exclude all other author access
- update_user_meta on registration but only for default role type
- Grant access to admin menu?
- I want this code to work only for Authors AND Contributors
- New folder and file permissions are not correct
- Disable Woocommerce checkout based on user role [closed]
- Adding a new role with administrator’s capabilities
- WordPress users roles wp-admin
- User roles empty