Support

Account

Home Forums Add-ons Repeater Field Relationship field in Repeater

Helping

Relationship field in Repeater

  • I have a situation where I display choices for names of people in a photo. The user may select from the list of people, or ;eave blank if they don’t know who is in the photo.

    I the want to insert a default person if they leave it blank. Using the acf/save_post hook, I am checking the contents of the repeated relationship field, but am not sure how to insert the details if the field is blank.

    The code I’m testing at the moment is ;
    if( have_rows(‘people’, $post_id) ):
    write_log(‘ACF FEF have_rows is set’);
    // Loop through rows.
    while( have_rows(‘people’, $post_id) ) : the_row();

    // Load sub field value.
    $sub_value = get_sub_field(‘family_member’, $post_id);
    if(isset($sub_value) || !empty($sub_value));
    write_log(‘ACF FEF family_member=’.json_encode($sub_value));

    // End loop.
    endwhile;

    else :
    write_log(‘$sub_value not set’,);
    endif;

    This returns an array of the field has been entered by the user, or is not set if then entered.

    I am not sure how to add the default values if not set by the user. Querying the table shows “a:1:{i:0;s:4:”1991″;}” for a field set by the user, but
    “[{“ID”:2200,”post_author”:”13″,”post_date”:”2022-07-04 16:12:04″,”post_date_gmt”:”2022-07-04 15:12:04″,”post_content”:””,”post_title”:”Another Tests Person – 01\/01\/2022″,”post_excerpt”:””,”post_status”:”publish”,”comment_status”:”closed”,”ping_status”:”closed”,”post_password”:””,”post_name”:”another-tests-person-01-01-2022″,”to_ping”:””,”pinged”:””,”post_modified”:”2022-07-05 10:35:52″,”post_modified_gmt”:”2022-07-05 09:35:52″,”post_content_filtered”:””,”post_parent”:0,”guid”:”https:\/\/the-harrisons.info\/family_members\/no-name-2\/”,”menu_order”:0,”post_type”:”family_members”,”post_mime_type”:””,”comment_count”:”0″,”filter”:”raw”}]”
    returned from the get_sub_field, so I am not sure what I need to use in an update_sub_field statement.

    ANy help or pointers would help

  • Solved. I forgot all about the default definition which sets the relationship field to object. Changing it back to ID fixes my issue

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

You must be logged in to reply to this topic.