Skip Navigation

[Resolved] Using Name iield data on post form to create title of custom post

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

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

Assisted by: Minesh.

Author
Posts
#2629635

You have helped us with this before, but we're now needing another version of this functionality.

We have created a post form called Free Agent Registration Form here:

hidden link

We are showing this form on this page:

hidden link

We have a custom post type called Free Agent with a field group defined here:

hidden link

The fields in the field group are Name, Session, and then a taxonomy called Tournaments.

All we want is for the Name field to be passed to be the post title of the Free Agent posts that are created by the form.

The code you previously supplied to us was to create the title for Match post types created with a different post form. That code is here:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

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('m-d-Y h:ia', $match_date_timestamp );
       $field_2 =$team1->post_title;
       $field_3 = $team2->post_title;
 
    $title = $field_1." - ".$field_2." vs. ".$field_3;
    $args = array('ID' => $post_id, 'post_title' => $title );
     wp_update_post($args);
    }
}

Can you help us modify this code as needed to only show the Name field as the post title? The post form is 1793, so I know 847 needs to be replaced with 1793 here. But I'm not sure what's needed to replace the remaining lines so only the name field is used as the post title.

#2629803

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please try to use the following code that should 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('m-d-Y h:ia', $match_date_timestamp );
       $field_2 =$team1->post_title;
       $field_3 = $team2->post_title;
  
    $title = $field_1." - ".$field_2." vs. ".$field_3;
    $args = array('ID' => $post_id, 'post_title' => $title );
     wp_update_post($args);

    } else if ($form_data['id']==1793) {

               $title = $_POST['wpcf-free-agent-name'];
              $args = array('ID' => $post_id, 'post_title' => $title );
               wp_update_post($args);


    }
}

Where:
As you may already noticed we added another condition using else if to check the form ID and adjusted the title code as given under:

 else if ($form_data['id']==1793) {

               $title = $_POST['wpcf-free-agent-name'];
              $args = array('ID' => $post_id, 'post_title' => $title );
               wp_update_post($args);


    }

Can you please try to use the above code and try to resolve your issue.

#2630085

I was planning to set this up as a stand-alone code snippet. Would this be correct for that?

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */
 
toolset_snippet_security_check() or die( 'Direct access is not allowed' );
 
// Put the code of your snippet below this comment.
 
add_action('cred_save_data','func_custom_free_agent_title',10,2);
function func_custom_free_agent_title($post_id,$form_data ) {
    if ($form_data['id']==1793) {
 
              $title = $_POST['wpcf-free-agent-name'];
             $args = array('ID' => $post_id, 'post_title' => $title );
              wp_update_post($args);
    }
}
#2630087

I just tried it using that and it worked. So thanks a bunch. This one is resolved.

#2630089

Thanks as always!