Skip Navigation

[Resolved] Set Title and Permalink when creating new child post

This support ticket is created 5 years, 8 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: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by laneV 5 years, 7 months ago.

Assisted by: Waqar.

Author
Posts
#1247357

My site is essentially a list of places (custom post type) and I have another custom post type which can hold images for the parent. I have a cred form that will upload the images and create the child post. The parent and child are in a one-to-one relationship. I have a separate cred form to edit the child post after it's created, I'm concerned here about when the child is created.

When the child is created via the cred form its name is always the same as the title of the cred form. I've tried to change this by using a cred_save_data action hook but it seems the form does not know the parent, yet it is able to associate the child to the parent.

I'm trying to figure out how to do 3 things:

1) I'd like the child post title to be the same as the parent post title, perhaps will some other text appended, so it's easy to see which place the child post is associated with.
2) I'd like the permalink to be created from my desired post title, not the forms title.
3) I'd like to be able to link back to the parent from the new child form - this seems challenging because the child has not been created yet.

#1247591

Hi Lane,

Thank you for contacting us and I'll be happy to assist.

Based on my tests, the newly created child post's title (through a new post form) uses default text like "CRED Auto Draft ...", when the post title field is not available or is left empty.

For points 1 & 2, you can extract the ID of the selected parent post from the "$_POST" variable ( ref: hidden link ), in a function attached to the "cred_save_data" hook.

Here is an example that worked on my website:


add_action('cred_save_data','custom_title_slug_func',15,2);
function custom_title_slug_func($post_id, $form_data) {
    if ($form_data['id']==204) {

        // set the values of fields in title and slug
        $title  = get_the_title($_POST['@shop-book_parent'])."-child";
        $slug   = sanitize_title($title).'-'.$post_id;
 
        $my_post_data = array(
            'ID'            => $post_id,
            'post_title'    => $title,
            'post_name'     => $slug,
        );
 
        // Update the post into the database
        wp_update_post( $my_post_data );
 
    }
}

Notes:

- Please replace "204" with the actual ID of your post form and "shop-book_parent" with the actual field name for your parent field.

- In this example, I've appended "-child" to the title and current post's ID to the slug to make it unique.

For point 3, if your goal is to redirect the visitor to the selected parent post after the form is submitted, you can use the "cred_success_redirect" hook to get the ID and the permalink for the parent post, using the same $_POST variable.
( ref: https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect )

Example:


add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
    if ($form_data['id']==204)
        $url = get_permalink($_POST['@shop-book_parent']);
   
    return $url;
}

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

#1273925

My issue is resolved now. Thank you!