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
- How to remove “MY WORDPRESS”” Label
- Compare “Main” post ID to ID inside wp_query loop
- Offset WP_Query by negative 1
- max_num_pages says 4, yet I am only able to navigate to the third page
- Custom Homepage Pagination using WP_Query
- WP_Query orderby random do not repeat infinite scroll – one loop
- How to build the WP_Query using the code?
- Find only those galleries with images
- My website is getting too many dierect home arechives and this is increasing my bounce rate
- WQ Query post meta date comparison
- Query posts by searching for a string in a meta field
- How to change tag based on metabox value within $wp_query
- Change User Role based on Point System Issue
- Category not being selected
- WP_Query with 4 post meta field arguments gives 500 internal server error
- how do I add role and capability after I create a new user
- Added Date Filter To Popular Posts Query
- Remove from array in WP_Query loop
- Paginate pages with dynamic query
- Phpmyadmin – post editing
- WP_Query loop doen’t work with my custom taxonomy
- Limit number of posts in WP_query per month
- How can I detect if the current post is in this loop?
- Advise on Templates for Custom Queries
- wp Query Posts to display in Nivo Slider
- WordPress | WP_Query does not return anything with s=a
- Get Authors Role
- Randomly pick one testimonial post
- How to get only ONE category of “Portfolio” posts to display on main page rather than ALL categories?
- How can I change my assigned user role in WordPress 3.5.1?
- filter posts by meta key with pagination
- Determining what post is displayed out of total posts found
- Trouble Making WP_Query paged
- How can I call a specific file (via php) by referencing the logged-in username?
- Single post – display list of all posts + specific post
- Reordering content using a meta value
- Search.php – return number of results but cannot loop through
- Tax_Query using WP_Query not working
- Role based permission edit for plugins
- How do I get the content of a custom instance of WP_Query?
- Performance concerns: index.php vs taxonomy-$taxonomy.php
- Creating ‘posts page’ loop based on the page itself
- Why does apply_filters behave different inside and outside a loop?
- Change post query onclick
- How to WP Query custom multiple custom taxonomies?
- How to add a recent post function to a text widget I created for practice?
- Can I make a search query which includes a space?
- WordPress Queries Not Working?
- WP_Query meta compare must include ALL array values
- Determine if ID is page or post and query the ID
- How to sort queried pages by an array of page ids?
- How to query for a page, get data, then query for child pages of that page
- Help with if statement inside while loop
- Excluded category from loop