假设您的产品是定制贴子,您可以这样做:
add_action(\'wp_insert_post\', \'do_stuff\'); // when product is created
add_action(\'save_post\', \'do_stuff\' ); // on product update
function do_stuff( $post_id ) {
// Making sure this runs only for products
$slug = \'product_post\';
if ( $slug != $_POST[\'post_type\'] ) {
return;
}
if (!get_post_meta( $post_id, \'new_price\', true )){
$old_price = get_post_meta( $post_id, \'old_price\', true );
update_post_meta($post_id, \'new_price\', $old_price);
}
}
更新旧产品让我们更新50个。
// url: site.com/test-page?offset=0, change this by 50 (0,50,100,150,200,250...)
$offset = $_GET[\'offset\']; // start with 0
$args = [
\'offset\' => $offset,
\'posts_per_page\' => 50,
\'post_type\' => \'product_post\'
];
$the_query = new WP_Query( $args );
if($the_query->posts){
foreach($the_query->posts as $product){
if (!get_post_meta( $product->ID, \'new_price\', true )){
$old_price = get_post_meta( $product->ID, \'old_price\', true );
update_post_meta($product->ID, \'new_price\', $old_price);
}
}
}else{
echo \'Done!\';
}