Hi there,
I'm hoping you can point me in the right direction with this one. I have a parent/child relationship between two CPTs.
The relationship is created by using a custom Post reference field assigned to the child CPT.
What I want admin to be able to do is via the WP dashboard, when they create a new child post, I automatically update the post title to include the title of the parent post.
I know I need to do this in my functions.php, using the save_post hook, but i'm unable to get the parent id, to get the parent title. I've tried this code to get the parent id: toolset_get_related_post( $post_id, 'monograph-update', 'parent');
That code returns the parent id when I run it outside of the save_post hook.
Is it possible to get the parent id, when saving a new child post using the save_post hook?
Hello and thank you for contacting Toolset support.
During the first save_post hook the connection between the child and parent posts is not yet saved. During subsequent saves, the link will be set, and the toolset_get_related_post will return the correct post.
Instead of relaying on the save_post hook, I suggest using the Toolset toolset_association_created hook.
add_action( 'toolset_association_created', 'my_update_post_title', 10, 5 );
function my_update_post_title( $association_slug, $parent_id, $child_id, $intermediary_id, $association_id ) {
// Check against the post reference field slug.
if ( $association_slug == "post-field" ) {
// execute the code to update the post's title
}
}
Read more about it here https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_association_created
I hope this helps. Let me know if you have any questions.
Exactly what I needed. Thanks! My issue is resolved now. Thank you!