I am trying to update multiple posts post meta at the same time. I have the following query:
<form action="" method="post">
<?php
$variations = new WP_Query();
$variations->query(array('showposts' => -1, 'post_type' => 'product_variation' )); while ($variations->have_posts()) : $variations->the_post(); ?>
<input name="regular_price[]" type="text" value="<?php echo get_post_meta(get_the_id(), "_regular_price", true); ?>" />
<input name="sale_price[]" type="text" value="<?php echo get_post_meta(get_the_id(), "_sale_price", true); ?>" />
<input name="item_id[]" type="hidden" value="<?php echo get_the_id(); ?>" />
<?php endwhile; wp_reset_query();?>
<input name="save" type="submit" />
I then have the following php to process the data:
<?php
if (isset($_POST['save'])) {
$ids = $_POST['item_id'];
$sales = $_POST['sale_price'];
foreach ($ids as $id){
update_post_meta($id,'_sale_price',$sale));
}
} ?>
For some reason the above does not save correctly. It will only save the last value, and apply this to all post meta. Is there something i am doing wrong?
I believe you need to add the id to
$sale
in you update_post_meta field. Like so:You forgot the } for “for”.
danyo, I feel you have issue with
$count
. Please make sure that this variable have proper count value to update data in loop.