Skip Navigation

[Resolved] Post Title from different type Fields not working with CRED

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

Problem:
How to create dynamic post title using CRED form having custom field values in title

Solution:
CRED offers hook cred_save_data using which you can generate your dynamic post title.

You can find proposed solution, in this case, with the following reply:
https://toolset.com/forums/topic/post-title-from-different-type-fields-not-working-with-cred/#post-900292

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 6 years, 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by Nashaat 6 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#899882

Hi,

I have followed Noman's instruction here : https://toolset.com/forums/topic/custom-post-title-from-5-different-custom-fields/page/2/#post-578300

This has worked for me and i could create now a post title from 4 different fields. The only problem is that the code works only if i create a new post from WordPress dashboard then i can i see the right title. but If i create a new post with CRED form from the front end, it generates a new Post with such title ( CRED Auto Draft d0f8b201bbf07f2b377cf4b7b72ef75d ). then i need to open the post in WordPress Dashboard and click UPDATE to show the right Title and then copy the new Title and change it in the post link too and save again.

Please let me know how to solve this!!

My Code:


add_filter( 'wp_insert_post_data' , 'modify_post_title' , '99', 2 ); // Grabs the inserted post data so you can modify it.
 
function modify_post_title( $data, $postarr )
{
    //$post_id = $postarr['ID'];
    if($data['post_type'] == '0km' && isset($postarr['wpcf']) ) { 
         
        $field_1 = $postarr['wpcf']['car-manufacturer'];
        $field_2 = $postarr['wpcf']['car-name'];
        $field_3 = $postarr['wpcf']['car-version'];
        $field_4 = $postarr['wpcf']['model-year'];
         
         
        $title = '';
        if(isset($field_1) && $field_1 != '')
            $title .= $field_1 . ' ';
        if(isset($field_2) && $field_2 != '')
            $title .= $field_2 . ' ';
        if(isset($field_3) && $field_3 != '')
            $title .= $field_3 . ' ';
        if(isset($field_4) && $field_4 != '')
            $title .= $field_4 . ' ';
        if(isset($field_5) && $field_5 != '')
            $title .= $field_5;
             
        $data['post_title'] =  $title ; //Updates the post title to your new title.
    }
  return $data; // Returns the modified data.
}

#900292

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

CRED offers hook cred_save_data using which you can generate your dynamic post title.

For example - add following code to your current theme's functions.php file:

add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data) {
 
if ($form_data['id']==9999) {

$field1 = get_post_meta($post_id, 'wpcf-car-manufacturer', true);
$field2 = get_post_meta($post_id, 'wpcf-car-name', true);
$field3 = get_post_meta($post_id, 'wpcf-car-version', true);
$field4 = get_post_meta($post_id, 'wpcf-model-year', true);
  
$post_title=$field1.'-'.$field2.'-'.$field3.'-'.$field4;

$slug = sanitize_title($post_title);
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
}
}

Where:
Replace 9999 with your CRED form ID.

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

#900891

Your code solved the problem in a second! Thank you Minesh