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?
- Group users by meta field, with name of meta field as group title
- Combining two wordpress queries with pagination is not working
- WP_Query using meta_query with relation OR and orderby meta_value doesn’t work
- view queries made?
- Get rewrite result
- WP_Tax_Query with post_tag not working
- How to show recent and upcoming birthdays using meta_query
- How to change posts order on Category and Tag archives?
- How can I retrieve comments from last 5 minutes for a post?
- Create a sitemap without a plugin – get parent pages only?
- WP_Query on custom field and order results
- WordPress: Display posts by custom select query
- Get ONLY the number of results in a query
- Include custom field in Ajax search WordPress
- Inserting serialized value into wp_postmeta using update_post_meta
- Display custom field meta outside loop, site wide
- Where is the WP_Query instance instantiated (when working with themes)?
- WordPress ignoring LIMIT / posts_per_page despite being in wp_query
- How change user type from contributor to author
- Conflict with get_posts and the_content
- Query Page Content From Theme Options?
- Filter wordpress post categories with jquery/ajax and pagination?
- How do I let contributors edit their posts after being approved once?
- How to Create a Random List of Child Pages
- Is there a way to make this kind of loop shorter and nicer?
- WP Query ordering on a meta field for posts with a date in the future not ordering by datetime
- Merge 2 custom post type posts and taxonomy terms and sort ascending
- Pagination on template page for custom query redirecting to index.php
- Fix ‘Add Role’ Option not there in wordpress 5.2.2
- Exec wp query in slow motion to avoid memory error?
- Remove post type filter added by the plugin in the final query
- How to get more data of a post by wp_query
- Query of all pages, including children, ordered by meta key
- WP_User_Query orderby meta_val_num
- Query Taxonomy By Page Title
- Unable to paginate a custom page query
- How to change the default post type over the loop?
- How to WP Query custom multiple custom taxonomies?
- How to display posts from custom post type category(custom Taxonomy) wise?
- Search filter between promo and exact price
- Bootstrap grid while loop
- search multiple keywords with wp_query using one query
- Order posts by tags count?
- Wp Query sort order
- get_posts query caching?
- Adding additional taxonomies to wordpress taxonomy page
- why doesn’t this pre_get_posts code work?
- Best way to Order Post in Home without a plugin
- Retrieving category pages from subcategory returns empty sets
- Loop through multiple custom fields with increasing number
- Different number of posts showing in development vs production server
- Get WP_Query query after execution?
- 294 Queries on Mainpage of WordPress
- How do I subquery with custom meta fields?
- Advanced Search – Is this possible?