Skip Navigation

[Resolved] Dynamic post title based off Toolset custom fields

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

Problem:

I want to auto-fill the post title for my “summit” posts from custom fields (year, organization, city, country) so it reads like “G6 2025 Xanadu Summit (Argentina)”, but my PHP attempts aren’t working.

Solution:

Add a cred_save_data hook that reads wpcf-summit-year, wpcf-summit-organization, wpcf-summit-host-city, and wpcf-summit-host-country, concatenates them into the desired string, and updates post_title and post_name; optionally restrict by form ID and post type.

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/

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 1 reply, has 1 voice.

Last updated by alexandreT 10 hours, 6 minutes ago.

Assisted by: Christopher Amirian.

Author
Posts
#2820621

Hi team Toolset! Looking for a bit of an assist here if possible? Thank you

1. Tell us what you are trying to do?

I'm trying to figure out how to have autofilled dynamic post titles for a series of summits, which would then allow me to publish the posts (Slug for type is 'summit')

I'm trying to get it to read like this:

G6 2025 Xanadu Summit (Argentina)

With my fields it would be something like this

(summit-year) (summit-organization) (summit-host-city) Summit ((summit-host-country))

2. Is there any documentation that you are following?

No, trying with PHP snippets with ChatGPT to no avail

3. Is there a similar example that we can see?
No.

4. What is the link to your site?
Not available yet

#2820701

Christopher Amirian
Supporter

Languages: English (English )

Hi,

Welcome to Toolset support. This will indeed be outside of our support scope as it is a pure PHP snippet creation.

I suggest the snippet below that you can add as a custom code:

add_action( 'cred_save_data', function( $post_id, $form_data ) {

    // Optional: limit to a specific post form ID
    // if ( (int) $form_data['id'] !== 12345 ) return;

    // Optional: also ensure we only touch the 'summit' post type
    if ( get_post_type( $post_id ) !== 'summit' ) return;

    // Types custom fields use meta keys wpcf-{field-slug}
    $year    = trim( (string) get_post_meta( $post_id, 'wpcf-summit-year', true ) );
    $org     = trim( (string) get_post_meta( $post_id, 'wpcf-summit-organization', true ) );
    $city    = trim( (string) get_post_meta( $post_id, 'wpcf-summit-host-city', true ) );
    $country = trim( (string) get_post_meta( $post_id, 'wpcf-summit-host-country', true ) );

    // Build: G6 2025 Xanadu Summit (Argentina)
    $parts = [];
    if ( $org )     $parts[] = $org;
    if ( $year )    $parts[] = $year;
    if ( $city )    $parts[] = $city . ' Summit';
    if ( $country ) $parts[] = '(' . $country . ')';

    if ( $parts ) {
        $title = trim( preg_replace( '/\s+/', ' ', implode( ' ', $parts ) ) );
        wp_update_post( [
            'ID'         => $post_id,
            'post_title' => $title,
            'post_name'  => sanitize_title( $title ),
        ] );
    }
}, 100, 2 );

Hook reference: https://toolset.com/documentation/programmer-reference/cred-api/

- Replace the meta keys if your field slugs differ (pattern is wpcf-{your-field-slug}).
- The code runs on create and edit submissions. If you only want to set the title the first time, wrap the wp_update_post() part in if ( get_the_title( $post_id ) === '' ).

The code above is a suggestion to help you continue the work. We did not test this on our installation.

Thanks.

#2820928

Thank you so much Christopher!