0

I am trying to create a function which either deletes all the postmeta with the meta_key "bakground-video", or edits the meta_value to empty it from the values. There might be multiple postmeta with the meta_key "background-video" and I want the function to run on all of them.

I will use this function on a multisite when a user changes role to "basic" this will fire and delete or edit all the postmetas with the key. How would I make this happen? All of the functions I can find when searching requires me to know the post_id, which I don't know.

This is what I have come up with after some searching, but I can't make it work!

    $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();

This is inserted in a function which fires when a user changes role to a role called "basic".

EDIT: Together with user role change function:

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 );
            if ( $all_ids->have_posts() ) {
                while ( $all_ids->have_posts() ) {
                    $all_ids->the_post();
                    update_post_meta( $p->ID, 'background-video', '' );
                }
            }
            wp_reset_postdata();


    }

}, 10, 3 );
0

Untested, but this might be what you're looking for:

$args = array( 'fields'          => 'ids',
               'posts_per_page'  => -1,
               'post_type'       => 'attachment',
               'meta_key'        => 'background-video'
               );
$all_ids = new WP_Query( $args );
if ( $all_ids->have_posts() ) {
    while ( $all_ids->have_posts() ) {
        $all_ids->the_post();
        update_post_meta( get_the_ID(), 'background-video', '' );
    }
}
wp_reset_postdata();

References

  • Thank you a lot Pat, but sadly I could not make it work. Check the original post, I edited it with the complete function, which should fire this when a user changes role to "basic", but nothing in the database was changed. So it doesn't work. – jockebq Apr 18 '18 at 18:44
  • Whoops, I didn't notice you were setting 'fields' => 'ids'. I've updated my code to reflect that. (Specifically I'm using get_the_ID() rather than $p->ID in the update_post_meta() call.) – Pat J Apr 18 '18 at 18:53
  • Thank you for trying to help, it is much appreciated! I tried your code, with the function I posted in the main post. But still nothing happens when switching user role. The postmetas with meta_key background-video still has a meta_value. – jockebq Apr 18 '18 at 18:55
  • I'm afraid it's time for you to debug, then. Usually I throw in a lot (like a lot) of var_dump()s as I go, to inspect every line to see where something's going wrong. For example, update_post_meta() will return a boolean true or false on success or failure, so you should probably start with something like $x = update_post_meta( {etc} ); var_dump( $x ); and see if it's false. At least then you'll know where the problem is, even if you're not sure what it is. – Pat J Apr 18 '18 at 19:18
1

Finally solved it, I think the issue was that I hade the wrong post_type. Now it works fantastically!

Thanks a lot for all the help!

add_action( 'set_user_role', function( $user_id, $role, $old_roles )
{
    if ( 'basic' == $role ) {
            $args = array( 'fields'          => 'ids',
                           'posts_per_page'  => -1,
                           'post_type'       => 'slide',
                           'meta_key'        => 'background-video'
                           );
            $all_ids = new WP_Query( $args );
            if ( $all_ids->have_posts() ) {
                while ( $all_ids->have_posts() ) {
                    $all_ids->the_post();
                    update_post_meta( get_the_ID(), 'background-video', '' );
                }
            }
            wp_reset_postdata();
    }

}, 10, 3 );
  • Yeah, having the wrong post_type in your query will make a difference. I'm glad you got it solved. – Pat J Apr 18 '18 at 20:32
  • Yes, so sorry for that mistake! And Thank you very very much!! – jockebq Apr 18 '18 at 20:32

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.