Skip Navigation

[Resolved] Auto Generate Post Title and Name/Slug by Combining Other Fields

This support ticket is created 5 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.

This topic contains 9 replies, has 2 voices.

Last updated by davidT-17 5 years, 6 months ago.

Assigned support staff: Minesh.

Author
Posts
#495687

I have read about a dozen of the other threads concerning auto-generated post titles, but have been unsuccessful in achieving the result.

Here is what we have in functions.php:
/**
* Create custom title from multiple fields
**/
add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data)
{
$field1 = get_post_meta($post_id, 'street_address', true);
$field2 = get_post_meta($post_id, 'city_address', true);
$field3 = get_post_meta($post_id, 'state_address', true);
$field4 = get_post_meta($post_id, 'zip_address', true);

$post_title="$field1 $field2 $field3 $field4";
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title));
}

In the CRED Form Editor we did the following to hide the Post Title Form Field on the Front End:
<div class="cred-field cred-field-post_title">
<div style="visibility:hidden;">
[cred_field field='post_title' post='vehicle' value='' urlparam='']</div>
</div>

Unfortunately, using the above code results in an error message when submitting the form on the front end and nothing is saved to the database. We also tried using <div style="display:none;"> in the CRED Form Editor and had the same result.

Thank you in advance for helping with this. We are really stumped and can't tell if it's one code snippet or both that has an error.

#495877

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Could you please try following code:

add_action('cred_save_data', 'build_post_title', 10, 2);
function build_post_title($post_id, $form_data)
{
$field1 = get_post_meta($post_id, 'wpcf-street_address', true);
$field2 = get_post_meta($post_id, 'wpcf-city_address', true);
$field3 = get_post_meta($post_id, 'wpcf-state_address', true);
$field4 = get_post_meta($post_id, 'wpcf-zip_address', 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));
}
#496271

Thank you Minesh. That snippet is taking care of combining those fields into a working post title. However, I still get an error on the form submission when using the following CSS to hide the title form field in CRED:

"In the CRED Form Editor we did the following to hide the Post Title Form Field on the Front End:
<div class="cred-field cred-field-post_title">
<div style="visibility:hidden;">
[cred_field field='post_title' post='vehicle' value='' urlparam='']</div>
</div>"

Do you know how we can "hide" the form field on the front-end so that users can't see it, while at the same time avoiding the error? Would changing the build_post_title function's action to occur before submission solve this? My thought there is that because WordPress requires a value for the title field before submitting, a pre-submission action would satisfy the requirement.

Thank you again for your help on solving this.

#496365

On top of the continued issue of hiding the Post_Title Form Field, we just noticed that the function for building the title is also applying to the child post which has a separate custom post type and needs to save as a simple text field with no auto-generation of the title. So my question is, can we have the build_post_title function target a single custom post type without affecting the child posts?

Thank you!

#496467

Minesh
Supporter

Languages: English (English )

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

Yes - you can bind the function to run based on your CRED form ID.

For example:

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-street_address', true);
$field2 = get_post_meta($post_id, 'wpcf-city_address', true);
$field3 = get_post_meta($post_id, 'wpcf-state_address', true);
$field4 = get_post_meta($post_id, 'wpcf-zip_address', 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.

#496832

Minesh,

Thank you for the help on targeting the cpt. I noticed that when I changed one of my fields to a taxonomy it no longer appears in the post title. I changed state_address to a taxonomy and kept the code in its most recent form but it appears to not recognize the value in the post_title.

Do I need to change this line of code? $field3 = get_post_meta($post_id, 'wpcf-state_address', true);

Specifically do I need to change 'wpcf-state_address'?

#497268

Minesh
Supporter

Languages: English (English )

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

Well - what is your taxonomy slug?

You can get the attached taxonomy terms to current post using following code:

$terms = get_the_terms( $post_id, $taxonomy_slug );

OR

$term_list = wp_get_post_terms($post->ID, 'product_features', array("fields" => "all"));
foreach($term_list as $term_single) {
echo $term_single->slug; //do something here
}

=> https://codex.wordpress.org/Function_Reference/wp_get_post_terms

#498055

Hi Minesh,

The taxonomy slug is "state-address"

I am having trouble understanding the concept in the Codex. Would I use $terms = get_the_terms( $post_id, $taxonomy_slug ); instead of $field3 = get_post_meta($post_id, 'wpcf-state-address', true);

What I mean is would my function look like this to call the term value for the taxonomy:

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-street_address', true);
$field2 = get_post_meta($post_id, 'wpcf-city_address', true);
$terms = get_the_terms( $post_id, $taxonomy_slug, 'wpcf-state-address');
$field4 = get_post_meta($post_id, 'wpcf-zip_address', true);

$post_title=$field1.'-'.$field2.'-'.$terms.'-'.$field4;
$slug = sanitize_title($post_title);
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
}
}

Thank you again Minesh for your guidance on this and apologies for being so new to all of this.

#498090

Minesh
Supporter

Languages: English (English )

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

get_post_meta() is used to retrieve the value of custom fields. If you want to retrieve the values of taxonomy terms attached to post:

To get post terms:

$term_list = wp_get_post_terms($post_id, 'state-address', array("fields" => "names"));
$terms = join("-",$term_list);

So your code should look like:

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-street_address', true);
$field2 = get_post_meta($post_id, 'wpcf-city_address', true);
$term_list = wp_get_post_terms($post_id, 'state-address', array("fields" => "names"));
$terms = join("-",$term_list);
$field4 = get_post_meta($post_id, 'wpcf-zip_address', true);

$post_title=$field1.'-'.$field2.'-'.$terms.'-'.$field4;
$slug = sanitize_title($post_title);
wp_update_post(array('ID'=>$post_id, 'post_title'=>$post_title,'post_name' => $slug));
}
}
#498510

Thank you so much Minesh!

You were a huge help 🙂