Skip Navigation

[Closed] Automatically connecting most recent child posts to a parent post

This support ticket is created 2 years, 9 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 7 replies, has 3 voices.

Last updated by Shane 2 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#2295547

Hi,

I have a parent post (Today) being automatically generated each day via code in my function file and the "WP Crontrol" plugin which allows me to run cron jobs. I'd like to automatically connect the most recent 2 child posts (Articles) to that parent post upon its generation.

I thought perhaps I could add code from this post here, but I'm stuck.
https://toolset.com/forums/topic/bulk-assign-child-posts-to-parent-post/#post-1247104

Can you assist with this?

This is the code I am currently using:

[code]
//Create Today post with one day interval
add_filter( 'cron_schedules', 'add_cron_interval_one_day' );
function add_cron_interval_one_day( $schedules ) {

$schedules['one_day'] = array(
'interval' => 86400, //# of seconds in 1 day
'display' => esc_html__( '1 days' ),
);

return $schedules;

}

// Schedule Cron Job Event
function custom_cron_job() {
if ( ! wp_next_scheduled( 'one_day_hook' ) ) {
wp_schedule_event( time(), 'one_day', 'one_day_hook' );
}
}
add_action( 'wp', 'custom_cron_job' );

//Insert post action
function create_post_w_random_terms() {

$postdate = $_POST['Y-m-d'];

$post_id = wp_insert_post( array(
'post_type' => 'today', //post type
'post_status' => 'publish',
'post_title' => 'Today - '. date('l, F jS, Y') , //Set this to whatever you want for post title
'post_content' => 'content here', //Same here
));

}
add_action( 'one_day_hook', 'create_post_w_random_terms' );
[/code]

Thanks,

Tim

#2295655

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Tim

Without looking very closely at your code, there is nothing there to connect posts using the relationships API.

Your code will want to get the two most recent articles (using get_posts: https://developer.wordpress.org/reference/functions/get_posts/).

For each of those, it will then want to connect those posts to the new parent, using toolset_connect_posts: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts.

#2297043

Right, I was hoping you could help out with that, I'm guessing I need something like the below code to get the 2 recent posts, but I have no idea how to incorporate that into my existing code and to then connect with toolset_connect_posts.

$args = array(
'numberposts' => 2,
'post_type' => 'print' //the custom post type name
'meta_query' => array( // to get posts that have the custom field done unselected
array(
'key' => 'wpcf-done',
'value' => '0',
)
);

This post seems to have a similar issue and a solution, but I'm just lost as how to put it all together. Any help would be really appreciated.

https://toolset.com/forums/topic/continued-connect-multiple-child-posts-to-parent-in-one-move/

Tim

#2299017

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

You should be able to get the recent posts like this.

$args = array(
'numberposts' => 2,
'post_type' => 'print' //the custom post type name
'meta_query' => array( // to get posts that have the custom field done unselected
array(
'key' => 'wpcf-done',
'value' => '0',
)
);
recent_posts = wp_get_recent_posts($args); //returns an array of post objects 
 foreach( $recent_posts as $post_item ) {

//code to connect post to relationship goes here.
}

Please let me know if this helps.
Thanks,
Shane

#2299063

Thanks, so then it looks like the final piece would be this?

toolset_connect_posts( 'today_print', $today_id, $post_id );

#2299143

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Timothy,

That's correct.

Of Course you will need to adjust the variable names to so they are passed correctly.

To get the ID of the post being passed by the foreach loop you will need to do $post_item['ID']

Thanks,
Shane

#2304567

Ok, thanks. But I don't really have any php knowledge on how to put together the original code that auto-generates the cpt, with the code to get the 2 recent posts and the code to connect post to relationship. I've just been piecing together code I've found. Would my best option now be to hire a Contractor on your job board to finish this up?

Tim

#2304859

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Tim,

Yes that would be the best option.

As they would be able to produce the completed code for you and perform adequate testing to ensure that it works for your exact use case.

To find a list of our contractors you can go to the link below.
https://toolset.com/contractors/

If there are any further questions please let me know.
Thanks,
Shane

The topic ‘[Closed] Automatically connecting most recent child posts to a parent post’ is closed to new replies.