Usermeta key that stores the user’s role has a prefix in the name, the one set in the wp-config.php
file. So meta_key
will have the form {prefix}_capabilities
and your query should look like
global $wpdb;
$wpdb->query(
$wpdb->prepare(
"UPDATE {$wpdb->usermeta} SET meta_value="a:1:{s:6:\"editor\";b:1;}" WHERE user_id = %d AND meta_key = '{$wpdb->prefix}capabilities'",
$user_id )
);
You do not need to write SQL query. You can achieve the same by fetching the WP_User
object with get_userdata
function and set new role with the method set_role (or remove_role
and add_role
to change only one of the roles).
$my_user_id = 12345; // user ID for changing role
$my_user = get_userdata($my_user_id);
if ( $my_user instanceof WP_User )
$my_user->set_role('editor');
Related Posts:
- SQL User Query by Multiple Roles using PHP
- How to display user order by role
- Get users query makes the site loading time too big?
- Getting all user metadata from user
- How to add custom query filters in WP_User_Query
- Hide posts from users with a specific role
- How to order posts by custom WP role?
- How to hide post content/meta from everyone except the post author and admin
- Filter user list that meta_value is empty
- Query wp_usermeta alongside wp_users
- WP User Query get all authors with last name starting with specific letter
- Sort users by “birthday” using WP_User_Query
- How to order posts in wp_query by the user role (2 roles in array)
- Hide Administrators From User List except current user (administrator)
- Import wp users via one click demo option
- Dynamic User Id
- How can I query users by custom role?
- Why does get_posts only show results for Admins or logged-out users?
- Prevent author bio page from showing in search results
- Get multiple users with meta value in one query and populate WP_User class
- Reduce number of SQL queries inside WP_Query loop to fetch author data
- Display if author page is author page of current user
- Get user first name from custom endpoint
- Show menus to one admin username
- Storing an array of objects related to each user
- Create one-use post dynamically, add to main query, do not insert post (user profile view)
- Link Author URL’s based on a custom field
- Query to get last posts from authors of blog
- Query pages created by deleted users
- Query posts by post type, author and post meta
- Random users always showing same 8 users
- Meta query orderby meta_value_num sorting by role first
- Pre_get_posts only show posts by administrator roles
- How to query 5 users in random who have published more than 10 posts
- How to query users by post count no less than 10
- Merge wp-query and get_users
- WP_Query: Query posts only if their access is restricted to logged user’s role
- Get all wp_users sort by metakey
- Logging search queries for logged-in users
- List users by Year of Birth using a foreach loop to dynamically populate years and data
- Show subscriber id in loop
- Displaying content to search engines but via navigation only for registered users.
- Query Users by post count, last 30 days and display each users post count according to post type
- Get all user with both meta_value
- How to know which editor published a post programmatically?
- Disallowing Users of a Custom Role from Deleting or Adding Administrators?
- What the user_status column?
- Exclude a category if post is only in that category using wp_Query
- Add Custom User Capabilities Before or After the Custom User Role has Been Added?
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- Use of caller_ get_ posts
- Tax_query terms ID’s using variable
- Order posts by tags count?
- Querying Multiple Custom Taxonomy Terms
- Unable to combine “search value” and “tax_query” using WP_Query
- How to extend tag and category “Related Posts” query to custom post_type if the first 2 terms have no posts
- WP_Query search for whole words
- WP_QUERY returns empty set when fired from a WP-CRON scheduled event
- WP_Query, tax_query and term_meta: How to?
- How to pass many ids in post__in?
- How do I run through a WordPress loop called from a filter function?
- WP_Query | Tax_Query Relation | Unable to use ‘OR’ as it then allows all products, help me finish my query?
- Get all fields inlcuding “ACF” (Advanced Custom Fields) columns in wp_query
- WP_query : Force last posts number over Reading Settings
- Order by most used tag
- How to display the number of posts in a category using WP Query
- Is wp_reset_postdata always necessary?
- wp_query: implicit paging? I get less posts that I should
- Query within a foreach within a query (queryception)
- WP Query Meta Value – How To Identify Specific, Unique Values?
- Shortcode Not Working in Slider When Added To Post Title
- Displaying pages on an archive page
- Multiple pagename WP Query
- How I can repeat 2 HTML templates in a WordPress Query?
- How to add array [closed]
- Loop categories by recent post
- List of users with email and role
- Meta query with compare by more than 2 fields
- Offset WP_Query by negative 1
- meta_query returning excluded result
- How to pass the current content of $wp_query to a new page?
- Passing a SQL query to the WP Query
- Why when I using infinity scroll the one post loads over and over?
- WP Query with meta queries
- ACF Post Object meta-query by title not ID
- Case insensitive ORDERBY in wpquery
- Sorting with meta_query and multiple, optional meta keys
- getting posts by tags
- WP_Query & Duplicate entries
- Query child posts with tax query on parents
- Include post_status check within $wpdb query
- Restrict Access to the User Profile
- Return on a quest all these meta_value
- Complicated WP_Query
- How to get only 1 role from user
- Custom query to show posts
- Taxonomy related query not working
- Multiple Orderby is not working right
- wp_query, calculate with two dates when ‘key’ is text format
- Use value from meta key array for use in WP_Query