Skip Navigation

[Resolved] Save post title with parent post name

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.

This topic contains 2 replies, has 2 voices.

Last updated by Aaron Zame 1 year, 1 month ago.

Assigned support staff: Jamal.

Author
Posts
#2148797

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?

#2148951

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

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.

#2149111

Exactly what I needed. Thanks! My issue is resolved now. Thank you!