Skip Navigation

[Resolved] Relationship of dynamically created child post

This thread is resolved. Here is a description of the problem and solution.

Problem: I am creating a post programmatically and I would like to link that post to another post using a post relationship.

Solution: Get the ID of the inserted post and use that in a toolset_connect_posts API call to connect the related posts.

//Create dynamic meeting room bookings
add_action('cred_submit_complete_2147', 'meeting_booking_times_submit',10,2);
function meeting_booking_times_submit($post_id, $form_data)
{
// Create post object
$my_post = array(
'post_title' => 'dynamic',
'post_status' => 'publish',
'post_type' => 'booking-times',
);

// Insert the post into the database
$my_post_id = wp_insert_post( $my_post );
toolset_connect_posts( 'meeting-room-booking-meeting-room-booking-times', $post_id, $my_post_id );
}

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by ahmedI 6 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1080658

Hi,

I'm trying to dynamically create post children while saving of the parent post. i need to set the relationship with the creation of the child post using the following code which is not working as expected:

add_action('cred_submit_complete_2147', 'meeting_booking_times_submit',10,2);
function meeting_booking_times_submit($post_id, $form_data)
{
// Create post object
$my_post = array(
'post_title' => 'dynamic',
'post_status' => 'publish',
'post_type' => 'booking-times',
);

// Insert the post into the database
wp_insert_post( $my_post );
toolset_connect_posts( 'meeting-room-booking-meeting-room-booking-times', $post_id, $my_post->ID );
}

Thanks.

#1080697

I think the problem is you're trying to get the post ID from $my_post, but $my_post is not a post object. It is an array of attributes you pass into the wp_insert_post function. Instead, you can check the return value of the wp_insert_post function.

Get the post ID like this:

$my_post_id = wp_insert_post( $my_post );
toolset_connect_posts( 'meeting-room-booking-meeting-room-booking-times', $post_id, $my_post_id );

Check the documentation here for an example:
https://developer.wordpress.org/reference/functions/wp_insert_post/#comment-401

#1081091

Hi Christian,

Thanks a lot for your help, what you suggested is absolutely right and everything is now working perfectly.

Here is the final code for later reference:

//Create dynamic meeting room bookings
add_action('cred_submit_complete_2147', 'meeting_booking_times_submit',10,2);
function meeting_booking_times_submit($post_id, $form_data)
{
// Create post object
$my_post = array(
'post_title' => 'dynamic',
'post_status' => 'publish',
'post_type' => 'booking-times',
);

// Insert the post into the database
$my_post_id = wp_insert_post( $my_post );
toolset_connect_posts( 'meeting-room-booking-meeting-room-booking-times', $post_id, $my_post_id );
}