Support

Account

Home Forums Bug Reports update_field() problem with relationship field

Helping

update_field() problem with relationship field

  • I have 2 CPT, “company” and “area”. Companies have a repeater field with the “areas” as a subfield, but Areas also have a custom relationship field for Companies. This may seem redundant, but I’ve found it’s the best thing to do in my case (but this isn’t the issue).

    Whenever someone updates or publishes a new Company, I want to grab the area subfield (an ID), then update the Company under that area id with the current company id. However, nothing seems to be updating at all. No errors, no nothing. What am I doing wrong?

    // After creating a company, programmatically add the company to the area
    function add_company_to_area($post_id) {
    
        if (get_field('location', $post_id)) {
    
            $companies_key = 'field_531f53271acd1';
            
            while(has_sub_field('location', $post_id)) {
        
                $area_id = get_sub_field('area')[0];
        
    
                update_field($companies_key, $post_id, $area_id);
            }
        }
    
    }
    
    add_action( 'save_post', 'add_company_to_area' );
  • Hi @anniesexton

    It is not possible to update a sub field, you must update the entire repeater field and is not advised for many efficiency related issues.

    Is there any other way you could better architect the data?

    Thanks
    E

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

The topic ‘update_field() problem with relationship field’ is closed to new replies.