I'm trying to update a postmeta field in the database called background-video. I want to clear the value from this when a user has changed the role to a user role called basic.
I found this code, which from what I understand runs when a user has changed role to the user role "basic".
add_action( 'basic', function( $user_id, $role, $old_roles )
{
// Your code ...
}, 10, 3 );
The problem is I don't know how to target all the postmeta fields called background-video and clear the meta-value from them.
EDIT: I tried this, but it is not working:
add_action( 'set_user_role', function( $user_id, $role, $old_roles )
{
if ( 'basic' == $role ) {
$args = array( 'fields' => 'ids',
'posts_per_page' => -1,
'post_type' => 'attachment',
'meta_key' => 'background-video'
);
$all_ids = new WP_Query( $args );
foreach( $all_ids as $ai ) {
update_post_meta( $ai->post->ID, 'background-video', '' );
}
wp_reset_postdata();
}
}, 10, 3 );
Found the function here How can update custom meta for all posts
One other thing is that I use this on a WP Multisite, and I let the users select from a few different roles, but I want this function to fire when they select "basic".