$old = get_post_meta($post_id, \'figure_sugsubject_repeatable_fields\', true);
$new = array();
$figuresugsubjectpositions = $_POST[\'figuresugsubjectposition\'];
$figuresugsubjectworkplaces = $_POST[\'figuresugsubjectworkplace\'];
$figuresugsubjectlocations = $_POST[\'figuresugsubjectlocation\'];
$figuresugsubjectfroms = $_POST[\'figuresugsubjectfrom\'];
$figuresugsubjectstatuss = $_POST[\'figuresugsubjectstatus\'];
$count = count( $figuresugsubjectpositions );
for ( $i = 0; $i < $count; $i++ ) {
if ( $figuresugsubjectpositions[$i] != \'\' ) :
$new[$i][\'figuresugsubjectposition\'] = stripslashes( strip_tags( $figuresugsubjectpositions[$i] ) );
$new[$i][\'figuresugsubjectworkplace\'] = $figuresugsubjectworkplaces[$i];
$new[$i][\'figuresugsubjectlocation\'] = $figuresugsubjectlocations[$i];
$new[$i][\'figuresugsubjectfrom\'] = $figuresugsubjectfroms[$i];
$new[$i][\'figuresugsubjectstatus\'] = $figuresugsubjectstatuss[$i];
endif;
}
if ( !empty( $new ) && $new != $old )
update_post_meta( $post_id, \'figure_sugsubject_repeatable_fields\', $new );
elseif ( empty($new) && $old )
delete_post_meta( $post_id, \'figure_sugsubject_repeatable_fields\', $old );
我有一个用于后期提交的前端表单。我希望表单中的新条目不会替换旧条目,而是希望添加新条目。我想问题出在update\\u post\\u meta上。我保存新条目而不是旧条目。不管怎样,我是不是保留了旧的,把新的和新的钥匙放在一起?
SO网友:birgire
请尝试替换:
update_post_meta( $post_id, \'figure_sugsubject_repeatable_fields\', $new );
使用:
add_post_meta( $post_id, \'figure_sugsubject_repeatable_fields\', $new );
添加更多与
figure_sugsubject_repeatable_fields
元键。
您还应该考虑使用filter_input()
而不是$_POST
.