Skip Navigation

[Resolved] Edit post title with data from post fields.

This support ticket is created 4 years, 8 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
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by Jamal 4 years, 8 months ago.

Assisted by: Jamal.

Author
Posts
#1608957

Tell us what you are trying to do?
Auto fill post title from data entered into post fields

Is there any documentation that you are following?
https://toolset.com/forums/topic/custom-post-title-from-5-different-custom-fields/

I've installed the Elementor Hello theme with the Hello Child theme, so I have placed these examples in the above post into my functions.php and changed them to my Post type slug and field slugs within the post type.

I have tried all examples in the above post, however, nothing seems to be working.

Any help would be greatly appreciated.

#1608985

Hello and thank you for contacting the Toolset support.

I'll need to take a closer look at your form and your custom code to be able to help. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **

#1609073

Thank you for the credentials. I confirm they are working for me.

But I'll still need to know which form? Where it is shown? And the custom code so I can understand what is the result that you want to achieve. You can share the code here or on pastebin.

#1609091

So I would like the 'caravans' type to show the following fields inside the post 'title';

I understand the slug for my post type is 'caravans'

Post form Fields: "'Caravan Year' 'Caravan Make' 'Caravan Model'"
Post form Slugs "'caravan-year' 'caravan make' 'caravan-model'"

Example:
"2020 Atlas Amethyst"

I originally put this into the functions.php within the Hello child theme.
I submitted 2 forms and the post title only shows the AUTO CRED title that CRED assigns to a post title that hasn't been submitted.

My first try was this;

add_filter ('title_save_pre','combine_title');
function combine_title($title) {
    global $post;
    $type = get_post_type($post->ID);
    if ($type == 'caravans') {
        $field_1 = get_post_meta($post->ID, 'caravan-year', true);
        $field_2 = get_post_meta($post->ID, 'caravan-make', true);
        $field_3 = get_post_meta($post->ID, 'caravan-model', true);
         
        $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 . ' ';
         
    }
return $title;
}

This didn't work. So I went on to try this;

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'] == 'caravans' && isset($postarr['wpcf']) ) { 
         
        $field_1 = $postarr['wpcf']['caravan-year'];
        $field_2 = $postarr['wpcf']['caravan-make'];
        $field_3 = $postarr['wpcf']['caravan-model'];
         
         
        $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 . ' ';

        $data['post_title'] =  $title ; //Updates the post title to your new title.
    }
  return $data; // Returns the modified data.
}

The above I believe should have changed the current (already created) posts to what I wanted the post title to show.

I don't know if I've missed something though.

Thank you for your help.

#1612251

Thank you for the details.

I would suggest using Toolset Forms hooks instead of the default WordPress hooks. You can use the cred_save_data hooks as follow:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==1911) { // <= Check the form ID, so this hook will be executed only for this form
        // Build the post title here
        // ...
        $args = array('ID' => $post_id, 'post_title' => $title, 'post_name' => '');
        wp_update_post($args);
    }
}

I think that when we pass an empty string ('') to post_name, WordPress will regenerate a slug for the post from the post title.

Please try this one and let me know what you will get. If it does not work, I'll check the code for any errors/issues.

#1618607

Hi Jamal,

Apologies for not getting back to your sooner, I have been super busy.

You mention

// Build the post title here

However, I don't know where to start when doing this 🙁

#1621141

Hello,

The codes on your reply (#1609091) are both building the title by concatenating values for three custom fields with different logics, the first one pulls the data from the database, the second one uses the data array from the hook.
On the 1st try: Code between lines 6-16 is building the title in $title var.
On the 2nd try: Code between lines 8-19 is building the title in $title var.

I would suggest copying code from the first try (lines 6-16) and use it to build the title. The code needs to be updated to use $post_id from the hooks arguments instead of the global $post->ID.

I am sure, you will understand, that custom coding is out of the scope of the support forum. If you are not comfortable writing code, you may consider hiring a developer. Check our partners here: https://toolset.com/contractors/
https://toolset.com/toolset-support-policy/