2013-11-26 58 views
1

我想同時更新多個職位後元。我有以下查詢:wordpress更新多個職位後元meta

<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" /> 

然後我有以下的PHP來處理數據:

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

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

foreach ($ids as $id){ 

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

} 
} ?> 

出於某種原因,上述方法不正確保存。它只會保存最後一個值,並將其應用於所有後期元。有什麼我做錯了嗎?

+0

@StreetCoder這是爲什麼? – danyo

+0

對不起,我錯過了,但我正在尋找解決方案 – StreetCoder

回答

3

我相信你需要在你的update_post_meta字段中添加id到$sale。像這樣:

<?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])); 

} 
} ?> 
0

你忘了「for」。

update .......; 
} 
} 
+0

對不起,錯過了這個....編輯問題。 – danyo

0

danyo,我覺得你有問題$count。請確保此變量具有適當的計數值以更新循環中的數據。

+0

$ count = $ variations-> post_count; – danyo

相關問題