Support

Account

Home Forums Front-end Issues Add to Relationship field programmatically?

Solved

Add to Relationship field programmatically?

  • I am creating an event based plugin and I have a registration form on the front-end where new attendees are created and associated to an even through a relationship field in the event cpt.

    I tried to use a get_field(relationship field) which is supposed to get an array of posts, push that newly created post into the array and update the relationship field but that did not seem to work out for me….

    I apologize if this has been answered before but I can’t seem to find it.

    //add attendee to event ateendee list (relationship)
    $event_id = $_POST['post_id'];
    $attendee_list = get_field( 'attendees_list', $event_id );
    $new_attendee = get_post( $attendee_id );
    				
    if( !is_array($attendee_list) ):
    	$attendee_list = array();
    endif;
    
    array_push( $attendee_list, $new_attendee );
    update_field( 'field_54811a48219fd', $attendee_list, $event_id );
  • Never mind, this code actually works.

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

You must be logged in to reply to this topic.