Support

Account

Home Forums General Issues Trying to automatically assign relationship using updatefield with insert_post Reply To: Trying to automatically assign relationship using updatefield with insert_post

  • The value of the ralationship field needs to be an array or all relationships, you can’t update them one at a time. Collect them in your loop and update them when the loop is completed.

    
    $relatie = $latestpost[0]->ID;
    $relatieID = get_field('week_palaver');
    $day_posts = array();
    foreach ($week as $weekday) {
      $PostID = wp_insert_post(array(
        'post_title'   => $latestpost[0]->post_title . ': ' . $weekday,
        'post_type'    => 'palaver',
        'query_var'    => true
      ));
      $day_posts[] = $PostID;
      wp_set_object_terms($PostID, $CatTermID, 'gebied');
    }
    update_field('field_5eea245f36217', $day_posts , $relatie);