Skip Navigation

[Resolved] Create title for post created through post from through submitted field data

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 7 replies, has 2 voices.

Last updated by brettB-4 1 year, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2604401

I found a thread on this subject here:

https://toolset.com/forums/topic/post-title-created-from-form-fields/

I was going to try following the instructions there, but I realized our need has something slightly different to it. One of our fields that we want to use in the title can be defined by the method provided in the code on this page. In our case that will be

$field_1 = $_POST['wpcf-match-date-and-time']

But the second and third field we need to use are ones where I'm not sure what to use. You can see our Post Form here:

hidden link

We want to use the Team 1 and Team 2 fields, but these are created through a relationship between our Match and Team content types. So I'm not sure what code I'd need to use for these two fields. Would it be as follows?

$field_2 = $_POST['wpcf-team-1']
$field_3 = $_POST['wpcf-team-2']

Also, I'm looking at this line of the code provided

add_action('cred_save_data','func_custom_post_title',10,2);

Would I need to change that to:

add_action('cred_save_data','func_custom_post_title',10,3);

in order to use three fields in the title? I'm not sure what the 10,2 refers to, but thought I'd ask in case I do need to change that.

#2604433

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

I checked the problem URL you shared but I do not see the form: hidden link
Maybe it requires login to see the form.

What if you try to use the following code. You can add the following code to "Custom code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/#adding-custom-php-code-using-toolset

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {

if ($form_data['id']==99999) {

$field_1 = $_POST['wpcf-match-date-and-time'];
$field_2 = $_POST['wpcf-team-1'];
$field_3 = $_POST['wpcf-team-2'];

$title = $field_1."-".$field_2."-".$field_3;
$args = array('ID' => $post_id, 'post_title' => $field_1.' '.$field_2);
wp_update_post($args);

}
}

Where:
- change 99999 with your original form ID

#2604571

Try going to the URL again now. I had the form access set to admin only. I've added it for guest users temporarily so you should be able to see it now. I'll let you see the form before I try the code. I'm just not sure about wpcf-team-1 and wpcf-team-2 being correct for these fields.

I have 99999 changed to 847.

#2604617

Minesh
Supporter

Languages: English (English )

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

I see there is two related parents added team1 and team2 and the custom date field.

Can you please try to use the following code and check if that help you to resolve your issue.

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data ) {
    if ($form_data['id']==847) {
        
       $team1_id = $_POST['@team-1-match_parent'];
       $team2_id = $_POST['@team-2-match_parent'];
       $team1 =  get_post($team1_id );
       $team2 =  get_post($team2_id );

        $match_date_timestamp = get_post_meta($post_id, 'wpcf-match-date-and-time', true);

       $field_1 = date('Y-m-d H:i:s', $match_date_timestamp );
       $field_2 =$team1->post_title;
       $field_3 = $team2->post_title;

    $title = $field_1."-".$field_2."-".$field_3;
    $args = array('ID' => $post_id, 'post_title' => $title );
     wp_update_post($args);
    }
}
#2604845

OK, I've added this code to the custom code section. Before I test, I have one more question. In the Post Form, we still have the Match Title field in there. We do not need the person filling out this form to provide a Match Title if your code works. So how would you suggest handling that? Should I just delete that field from the form or is it necessary for it to still be there for the post creation process?

#2605193

Minesh
Supporter

Languages: English (English )

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

Yes you can delete the post title and you should not face any issues.

#2605357

That worked! All good. Thanks again.

#2605359

My issue is resolved now. Thank you!