Support

Account

Home Forums General Issues Automating Shortcodes to ACF conversion Reply To: Automating Shortcodes to ACF conversion

  • Hi again James

    Some of my post have more than one before|after shortcode.
    I’ve built a huge array of hundreds of comparisons that I’ll loop through, like this:

    $BAcomparisonArray = array(
    array(
    	'postID'			=> 7651,
    	'beforeImage'		=> 'photo-7D-IMG_2201-RAW.jpg',
    	'afterImage'		=> 'photo-7D-IMG_2201.jpg'
    ),
    array(
    	'postID'			=> 7729,
    	'beforeImage'		=> 'photo-7D-IMG_2198-RAW.jpg',
    	'afterImage'		=> 'photo-7D-IMG_2198.jpg'
    ),
    array(
    	'postID'			=> 7729,
    	'beforeImage'		=> 'photo-7D-IMG_2201-RAW.jpg',
    	'afterImage'		=> 'photo-7D-IMG_2201.jpg'
    ),
    array(
    	'postID'			=> 7995,
    	'beforeImage'		=> 'photo-7D-IMG_2519-RAW.jpg',
    	'afterImage'		=> 'photo-7D-IMG_2519.jpg'
    ),
    array(
    	'postID'			=> 7995,
    	'beforeImage'		=> 'photo-7D-IMG_2822-RAW.jpg',
    	'afterImage'		=> 'photo-7D-IMG_2822.jpg'
    ),
    array(
    	'postID'			=> 8008,
    	'beforeImage'		=> 'photo-7D-IMG_2519-RAW.jpg',
    	'afterImage'		=> 'photo-7D-IMG_2519.jpg'
    ));

    But as you can see, for the posts where I have multiple before|after, the subarray appear multiple times with the same ID.

    With my actual code (see below), only one before|after is added to my flexible content, while I would obviously need as many as necessary:

    $value = array(
    	array('before_image' => $before_image, 'after_image' => $after_image, 'acf_fc_layout' => 'before_after')
    );
    update_field($field_key, $value, $postID);

    Is there a way to force the update field to create new entries?
    Example, for ID 7729, the first will act as today by creating a new layout and populating it, but when my loop run through another array with the same ID, I would like a new layout to be created, not just updating my previous one.

    I know that if I put as many arrays as necessary in my $value, the related layouts will be created for a same post (see below), but I don’t see quite well how to do this from $BAcomparisonArray.

    $value = array(
    	array('before_image' => $before_image, 'after_image' => $after_image, 'acf_fc_layout' => 'before_after'),
    	array('before_image' => $before_image, 'after_image' => $after_image, 'acf_fc_layout' => 'before_after'),
    	array('before_image' => $before_image, 'after_image' => $after_image, 'acf_fc_layout' => 'before_after'),
    	array('before_image' => $before_image, 'after_image' => $after_image, 'acf_fc_layout' => 'before_after')
    );