2

How can update custom meta (likes) for all posts that have this meta?

If i used something like this:

update_post_meta( $post_id, "likes", 1 );

it's required to add the post id that I need to update its meta.

2

How can update custom meta (likes) for all posts that have this meta?

If you want to update every post that has this meta:

See the returns field parameter and custom field parameters for Wp_Query to 1) retrieve only posts with that meta_key, and 2) to lighten query and only return the ids instead of full objects. Then loop through them.

Something like this (untested):

$args = array( 'fields'          => 'ids', 
               'posts_per_page'  => -1, 
               'post_type'       => 'posts', 
               'meta_key'        => 'likes' 
               );
$all_ids = new WP_Query( $args );

foreach( $all_ids as $ai ) {

   update_post_meta( $ai->post->ID, 'likes', 1 );
}
wp_reset_postdata();
  • I'm trying to use this code for editing the meta_value of all postmeta with meta_key "background-video". I want to edit the existing value of all those, clearing every value. I have tried using your code, here: wordpress.stackexchange.com/questions/300905/… without any success! – jockebq Apr 16 '18 at 17:49

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.