Inserting Posts programmatically with ACF relationship custom field

Question

I am trying to insert a post with type registration. I have a custom field with a relationship to a single event. After inserting I the count of registration posts = 1 but if I loop through the registrations on a page I can see two. Is there something else I need to do after inserting with wp_insert_post(). The custom field ‘event_id’ also doesn’t show the inserted relationship in WP-ADMIN, but it seems to be there in database.

    echo "<p>Count of posts is " . have_posts() . '</p>';
    while(have_posts()){
        the_post();
        echo "<p>event id is=" . get_field('event_id') . '</p>';
        print_r(get_field('event_id'));
        //the event_id seems to be ok so why isn't it working.
      }

Count of posts is 1

event id is=Array

Array ( [0] => WP_Post Object ( [ID] => 144 [post_author] => 1 [post_date] => 2020-06-01 17:58:28 [post_date_gmt] => 2020-06-01 16:58:28 [post_content] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Nec dui nunc mattis enim. Quis blandit turpis cursus in. Ac feugiat sed lectus vestibulum mattis. Tellus in hac habitasse platea dictumst vestibulum. Ut porttitor leo a diam sollicitudin. Facilisi nullam vehicula ipsum a arcu cursus vitae. Pharetra pharetra massa massa ultricies mi. Ullamcorper eget nulla facilisi etiam dignissim diam. Iaculis urna id volutpat lacus laoreet non curabitur. Elit eget gravida cum sociis natoque penatibus. [post_title] => Event 1 [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => event1 [to_ping] => [pinged] => [post_modified] => 2020-06-08 11:06:16 [post_modified_gmt] => 2020-06-08 10:06:16 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/ABC/?post_type=event&p=144 [menu_order] => 0 [post_type] => event [post_mime_type] => [comment_count] => 0 [filter] => raw ) )
event id is=Array

Array ( [0] => WP_Post Object ( [ID] => 144 [post_author] => 1 [post_date] => 2020-06-01 17:58:28 [post_date_gmt] => 2020-06-01 16:58:28 [post_content] => Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Nec dui nunc mattis enim. Quis blandit turpis cursus in. Ac feugiat sed lectus vestibulum mattis. Tellus in hac habitasse platea dictumst vestibulum. Ut porttitor leo a diam sollicitudin. Facilisi nullam vehicula ipsum a arcu cursus vitae. Pharetra pharetra massa massa ultricies mi. Ullamcorper eget nulla facilisi etiam dignissim diam. Iaculis urna id volutpat lacus laoreet non curabitur. Elit eget gravida cum sociis natoque penatibus. [post_title] => Event 1 [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => event1 [to_ping] => [pinged] => [post_modified] => 2020-06-08 11:06:16 [post_modified_gmt] => 2020-06-08 10:06:16 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/ABC/?post_type=event&p=144 [menu_order] => 0 [post_type] => event [post_mime_type] => [comment_count] => 0 [filter] => raw ) )

0
PeterB 8 months 0 Answers 87 views 0

Leave an answer