Skip Navigation

[Resolved] Save post title with parent post name

This support ticket is created 3 years, 3 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

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

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!