Skip Navigation

[Resolved] Unique URLs for Specific CPTs Created Though CRED

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

Problem: I would like to use a unique number as the title and URL slug of all posts created by a CRED form.

Solution: Use the cred_save_data hook to update the post with the correct information:

add_action('cred_save_data', 'auto_title_action',10,2);
function auto_title_action($post_id, $form_data)
{
    $forms = array( 123, 456 );
    // if a specific form
    if ( in_array( $form_data['id'], $forms ) )
    {
        $my_post = array(
            'ID'               => $post_id,
            'post_title'   => $post_id,
            'post_name' => $post_id
        );
 
        // Update the post in the database
        wp_update_post( $my_post );
    }
}

Replace 123, 456 with a comma-separated list of forms where this automatic title should be applied.

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/wp_update_post

This support ticket is created 7 years, 1 month 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

This topic contains 2 replies, has 2 voices.

Last updated by jonB-5 7 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#579886

I have six Custom Post Types, as follows...

Vessel, Voyage, Ticket, Ticket Variation, Booking, Passenger

For Vessels, Voyages and Bookings the URLs are .../post-type/post-title which is not a problem, but I would like the URLs for the other CPTs (Ticket, Ticket Variation and Passenger) to be set up as .../post-type/unique-number.

I would need that unique number to be automatically generated when a user creates a new post, instead of them having to decide it themselves.

Our set up is such that a user will create these new posts through CRED forms.

Is the above possible, and if so, can you advise on how to achieve it?

It would not be a problem if that unique number was generated as the Post Title, as we could move its current use (as a post name) to a custom field in the Post Type instead.

Note if php is required, examples would be very useful, thanks!

#580033

Sure, here's an example of using the post ID (which will always be a unique number when creating a new post) as the post title and slug:

add_action('cred_save_data', 'auto_title_action',10,2);
function auto_title_action($post_id, $form_data)
{
    $forms = array( 123, 456 );
    // if a specific form
    if ( in_array( $form_data['id'], $forms ) )
    {
        $my_post = array(
            'ID'               => $post_id,
            'post_title'   => $post_id,
            'post_name' => $post_id
        );

        // Update the post in the database
        wp_update_post( $my_post );
    }
}

Replace 123, 456 with a comma-separated list of form IDs where this code should be applied. Then in each of the CRED forms, remove the input field for the post title.

#580039

That's done it, thanks!