1

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".

0

Well, you're not entirely correct. This code will run, when the action called basic will be called using do_action('basic', ...). But... There is no such action in WP core, so your code won't run at all, I'm afraid.

There is hook that might be helpful in your case. It's called set_user_role and it has 3 params ($user_id, $role, $old_roles).

So your code should look more like this:

add_action( 'set_user_role', function( $user_id, $role, $old_roles ) 
{
    if ( 'basic' == $role ) {
        // Your code ...
    }

}, 10, 3 );
  • Thank you for noticing! Great to know how that works. I tried the part to update the postmeta with empty meta_value, this way without success: update_post_meta( $post_id, 'background-video', '', $prev_value ); – jockebq Apr 16 '18 at 11:25
  • And what is $post_id? Is it defined anywhere in your function? – Krzysiek Dróżdż Apr 16 '18 at 11:29
  • Post ID doesn't matter, I want to edit all meta_value (make them empty) from all meta_key called "background-video". So Post ID doesn't matter. – jockebq Apr 16 '18 at 11:33
  • Ok. Stop. You can’t just make some wish how the function should work and hope that it will. The $post_id param of update_post_meta is required, so you have to put some ID of existing post in there... – Krzysiek Dróżdż Apr 16 '18 at 11:35
  • But if I don't know the post ID before and want to target all meta_key background-video without knowing the post id? – jockebq Apr 16 '18 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.