danyo
danyo

Reputation: 5856

wordpress update multiple posts post meta

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?

Upvotes: 1

Views: 3444

Answers (3)

user3087856
user3087856

Reputation:

I believe you need to add the id to $sale in you update_post_meta field. Like so:

<?php
if (isset($_POST['save'])) {

    $ids = $_POST['item_id'];
    $sales = $_POST['sale_price'];

foreach ($ids as $id){

    update_post_meta($id,'_sale_price',$sale[$id]));

}
} ?>

Upvotes: 4

Ram Sharma
Ram Sharma

Reputation: 8819

danyo, I feel you have issue with $count. Please make sure that this variable have proper count value to update data in loop.

Upvotes: 0

Poomrokc The 3years
Poomrokc The 3years

Reputation: 1099

You forgot the } for "for".

update .......;
}
}

Upvotes: 0

Related Questions