wordpress 更新多个帖子帖子元

问题描述 投票:0回答:3

我正在尝试同时更新多个帖子帖子元。我有以下疑问:

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

}
} ?> 

由于某种原因,上述内容无法正确保存。它只会保存最后一个值,并将其应用于所有帖子元。我是不是做错了什么?

php wordpress metadata
3个回答
4
投票

我相信您需要将 id 添加到 update_post_meta 字段中的

$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,我觉得你对

$count
有意见。请确保该变量具有正确的计数值以更新循环中的数据。

© www.soinside.com 2019 - 2024. All rights reserved.