Support

Account

Home Forums ACF PRO Relationship field save only last value Reply To: Relationship field save only last value

  • I just had some time to take a close look at how a relationship field works. When you add an item to the list an li element is added to the selected items. Each of these li items includes a hidden field that has the ID of the post as it’s value. So, there isn’t a single field that contains all of the values but a separate hidden field for each value.

    In order to get all of the input fields that hold the real values of a relationship field

    
    var $inputs = $('.acf-input .acf-relationship .values input[type="hidden"]');
    

    would return a list of input elements.

    The main problem I see with this part of your code

    
    var form_data = {'action' : 'acf/validate_save_post'};
    $('#'+id+' > form#post :input').each(function(){
      form_data[$(this).attr('name')] = $(this).val();
    });
    console.log(form_data);
    

    is that the name attribute of each of these hidden fields is exactly the same, example: acf[field_574e05c19e01e][] so I’m guessing that each one overwrites the previous value which would explain why you are only getting the last value. Somehow in your loop you’re going to need to detect that the field values is part of an array and append it to the values already collected rather than set it.

    Since the user fields works in a similar manner I say that’s also the problem there.