Look at the remove_cap
method from WP_User
class-
/**
* Remove capability from user.
*
* @since 2.0.0
* @access public
*
* @param string $cap Capability name.
*/
public function remove_cap( $cap ) {
if ( ! isset( $this->caps[ $cap ] ) ) {
return;
}
unset( $this->caps[ $cap ] );
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
$this->update_user_level_from_caps();
}
Here at the documentation it is said that the parameter must be string. But you passed array. So the updated code should be like below-
function the_dramatist_set_capabilities() {
$user = new WP_User( 9 );
$caps = array('edit_posts', 'publish_posts', 'read');
foreach ($caps as $cap) {
$user->remove_cap( $cap );
}
}
add_action( 'init', 'the_dramatist_set_capabilities' );
Hope the above code will fix your problem. And always prefix your function with a unique keyword like here I’ve prefixed it with the_dramatist_.