Support

Account

Home Forums Add-ons Repeater Field ACF add_row works incorrect

Solved

ACF add_row works incorrect

  • I have a repeater ‘location_repeater’ with two subfields:
    – location_select (Post object)
    – available_sports (multiple Select)

    And I want to save data to repeater with my custom handler, where I use ACF add_row() function http://www.advancedcustomfields.com/resources/add_row/

    According to docs I should use field’s key rather than it’s name to allow ACF to correctly find the field if no existing value has been saved.

    Here is the code which I use to save a field row

    $row_key = array(
      'field_565eadf99195b' => $row['location_select'],
      'field_565eae049195c' => $row['available_sports']
    );
    $row_id = add_row('field_565eadcd9195a', $row_key, $post_ID);

    Where ‘field_565eadcd9195a’ – ‘location_repeater’, ‘field_565eadf99195b’ – ‘location_select’ and ‘field_565eae049195c’ – ‘available_sports’.

    When I submit my form I see new postmeta values in SQL and function return new row ID. But I can’t see any results on front-end. In admin panel I can see that repeater has new rows, but they are empty also.
    There are such values in SQL for my post.

    locations_repeater - 1
    _0_field_565eadf99195b - 563
    __0_field_565eadf99195b - '' (empty)
    _0_field_565eae049195c - a:1:{i:0;s:9:"sport";}
    __0_field_565eae049195c - '' (empty)

    Also I tried to update repeater values from admin panel and everything goes fine. And after that my code started working nice. But only for this one post, any new post has the same problem which I mentioned before.

    I will appreciate if anybody can help me with this problem.

  • I’m not sure there’s enough information to figure this out.

    
    $row_key = array(
      'field_565eadf99195b' => $row['location_select'],
      'field_565eae049195c' => $row['available_sports']
    );
    $row_id = add_row('field_565eadcd9195a', $row_key, $post_ID);
    

    can you provide more code to show where the array $row and the value of $post_ID are being set?

  • In my code $row is just an array

    $row = array(
      'location_select' => 603,
      'available_sports' => array('sport')
    );

    $post_ID is an integer = 2575 – for example.

  • I did a test where I set up a repeater with a post object field and a multi select as sub fields, added code to create a new row and it worked as expected. My code was extremely simple, it just inserted a new row on the current post when you visit the page on the front end.

    
    $post_ID = $post->ID;
    $row = array(
      'field_56c3a78ded331' => 196,
      'field_56c3a7c0ed332' => array(1,3,5)
    );
    $row_id = add_row('field_56c3a77aed330', $row, $post_ID);
    

    This leads me to believe that there is something that’s interfering with ACF. Especially since you say these are the values you say are being inserted.

    
    locations_repeater - 1
    _0_field_565eadf99195b - 563
    __0_field_565eadf99195b - '' (empty)
    _0_field_565eae049195c - a:1:{i:0;s:9:"sport";}
    __0_field_565eae049195c - '' (empty)
    

    What should be in the DB after the insert is this:

    
    locations_repeater - 1
    _locations_repeater - field_565eadcd9195a
    locations_repeater_0_location_select - 563
    _locations_repeater_0_location_select - field_565eadf99195b
    locations_repeater_0_available_sports - a:1:{i:0;s:9:"sport";}
    _locations_repeater_0_available_sports - field_565eae049195c
    

    So we need to figure out what’s causing this to happen. You’ll need to start by disabling plugins and switching to a default theme. If you can figure out where the interference is coming from then we might be able to figure out how to fix it.

  • Hi John, sorry for a long delay.
    I tried to turn off all plugins but problem still the same.
    So I have installed new wordpress and add there only one plugin ‘ACF pro’. I used ‘Twenty Fourteen’ theme and added there page with this code:

    <?php
    /**
     * Template Name: ACF test page
     */
    get_header(); ?>
    <form method="POST" action="<?php the_permalink(); ?>" style="float: right;width: 200px;">
    	<input type="hidden" name="field_key" />
    	<input type="text" name="sub_field_one" />
    	<input type="text" name="sub_field_two" />
    	<input type="submit" value="Try it!" />
    </form>
    <div style="float: right">
    <?php if($_POST): ?>
    <?php
    $post = array(
    		'post_title' => 'Title of new post',
    		'post_content' => '',
    		'post_status' => 'publish',
    		'post_type' => 'post'
    );
    $post_ID = wp_insert_post($post);
    
    $row = array(// don't forget to change field keys
    	'field_56d6bdeb338e7' => $_POST['sub_field_one'],
    	'field_56d6be03338e8' => $_POST['sub_field_two']
    );
    $row_id = add_row('field_56d6bdd7338e6', $row, $post_ID);
    echo $row_id;
    ?>
    <?php endif ?>
    </div>
    <?php
    get_sidebar();
    get_footer();

    Also I added ACF repeater with two text fields. So form on this page should create a new post and add there values from form to repeater subfields.
    After submitting I saw an ID of new row – ‘1’ near form. But in admin panel I can’t see any value which I wrote in form.
    Can you try it on your wordpress?

  • I added exactly your code to a test site except that I changed the field keys to the fields I created for the test. It worked for me as is should. It created a post with one row in the repeater field containing the values that I entered.

    Now you have me wondering. I didn’t ask and I looked over the topic and you did not say, what version of ACF are you using?

  • My version was 5.3.2.2, everything works good after update. Thank you John for your help and time.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘ACF add_row works incorrect’ is closed to new replies.