如何为所有拥有此meta的帖子更新自定义meta(likes)?
If you want to update every post that has this meta:
请参见
returns field parameter 和
custom field parameters 对于
Wp_Query
至1)仅检索具有该
meta_key
, 2)减轻查询的负担,只返回
ids
而不是完整对象。然后循环通过它们。
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();