Support

Account

Home Forums ACF PRO Copy relationship field values to another relationship field

Solved

Copy relationship field values to another relationship field

  • Hi there!

    I am making a backend with several relationship fields. A client wants it to be possible to copy the selected values from one relationship field to another instead of adding them by hand.

    I think that the pseudocode for a function like that would be relatively simple:
    1. Choose a field to copy values from via dropdown.
    2. A jquery script reads all selected values from the chosen field and adds them to the current field.

    My problem is that I am not familiar with the relevant ACF functions well enough. Which function(s) should I use to render the values in the field I am copying the values to?

  • Nevermind, I solved the problem in a much easier way.
    In case someone else needs a similar solution, here is the code for solution:

    jQuery(document).ready(function($){
      function ivab_copy_field_values(source_field_id, target_field_id) {
        $(source_field_id).find('.values li input').each(function() {
          fieldValue = $(this).val();
          targetLi = $(target_field_id).find('.choices li span[data-id="'+fieldValue+'"]');
          if ($(targetLi).length) {
            $(targetLi).trigger('click');
          }
        });
      }
    
      if ($('#normal-sortables').length) {
        $('.acf-field-56c3703fffcbd .acf-label')
          .append('<button class="button button-large copybutton" type="button" data-targetfield="#acf-field_56c3703fffcbd" data-sourcefield="#acf-field_56c372ebcec74">Copy from B side</button>');
        $('.copybutton').on('click', function() {
          console.log($(this));
          sourceField = $(this).attr('data-sourcefield');
          targetField = $(this).attr('data-targetfield');
          ivab_copy_field_values(sourceField, targetField);
        });
      }
    });
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Copy relationship field values to another relationship field’ is closed to new replies.