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

I have a frontend form for post submission. I want the new entries in the form not to replace the old ones, instead I want it to be added. I guess the problem is with the update_post_meta thing. I saves the new entries over the old. Is there anyway I keep the old ones and put the new ones with new keys?

1 Answer 1

0

Please try to replace:

update_post_meta( $post_id, 'figure_sugsubject_repeatable_fields', $new );

with:

add_post_meta( $post_id, 'figure_sugsubject_repeatable_fields', $new );

to add more values related to the figure_sugsubject_repeatable_fields meta key.

You should also consider using filter_input() instead of $_POST.

4
  • Thank you. That's correct but the problem is when add_post_meta, I no longer able to update/ delete existing ones. Any clue?
    – nidaelias
    Aug 24, 2014 at 17:06
  • I thought you didn't want to delete the existing ones ;-) Are you talking about another edit form?
    – birgire
    Aug 25, 2014 at 2:32
  • It is the same form. Well, yes I don't want to delete the existing ones, but at the same time I want them to be edited and updated. The thing is that I have made the form open to all users for the same post. Every user can come and make edits, but instead of replacing the existing ones, I want them to add/ edit or delete upon their choice. I know it's not entirly clear but I can explain if someone can ask :)
    – nidaelias
    Aug 25, 2014 at 5:09
  • btw, I have tried array_merge($new, $old) but it's creating duplicates.
    – nidaelias
    Aug 25, 2014 at 5:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.