Skip Navigation

[Resolved] Auto Generate Child Post Title based on Parent Title

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

Problem: I have a one-to-many post relationship set up in Types. When I create a child post in wp-admin, I would like to automatically set the child post title based on the parent post's title, using save_post.

Solution: Use the post relationships API toolset_get_related_post to retrieve the parent post ID during the save_post hook when saving a child post. Then use WordPress API wp_update_post to set the child post title automatically.

$parent_id = toolset_get_related_post( $post_id, 'relationship-slug' );

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post
https://developer.wordpress.org/reference/functions/wp_update_post/
https://developer.wordpress.org/reference/hooks/save_post/

This support ticket is created 5 years 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)

Author
Posts
#1492755

Tell us what you are trying to do?

I have two post types - slugs are courses and course-date with a one-to-many relationship

When adding a course-date I would like to automatically set the post title to the post title of the parent courses

Is there any documentation that you are following?

I've tried following https://toolset.com/forums/topic/add-auto-generate-post-title-from-fields/ and taking out the date parts as I don't need those in the title but with no luck.

#1492767

Hello, please include the code you have tried so far for me to review. I'll take a look and give you some feedback. Thanks!

#1492773

Here's one I tried...

function auto_title_func( $post_id ) {
    // If this is just a revision
    if ( wp_is_post_revision( $post_id ) ||  get_post_type($post_id) != 'course-date')
        return;
    remove_action( 'save_post', 'auto_title_func', 999 );
    $my_post = array(
      'ID'           => $post_id,
      'post_title'   => get_the_title($_POST['_wpcf_belongs_courses'])
    );
 
    wp_update_post( $my_post );
    add_action( 'save_post', 'auto_title_func', 999 );
}
add_action( 'save_post', 'auto_title_func', 999 );
#1494175

Okay the _wpcf_belongs... method of retrieving a related post only worked prior to Types 3.0 and the new post relationships features. If you've started using the new post relationships, the proper way to access a parent post is the toolset_get_related_post API: https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post

Here's an example:

$parent_id = toolset_get_related_post( $post_id, 'relationship-slug' );

You would replace relationship-slug with the slug of your O2M relationship.

Note that it's possible to save a child post without selecting a parent post, so your code should account for that case.

#1494223

Brilliant, that did the trick 🙂