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.
1 Answer
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();