Skip Navigation

[Gelöst] Auto-generate Post Title from custom field values

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem:
Client wants to auto-generate post titles in the back-end based upon the values of two custom fields (First and Last names)

Solution:
Only possible with custom code, which is not supported, but an example of how to achieve this would be:

/**
 * Auto-generate post title
 */
function tssupp_autogenerate_title( $post_id, $post ){
   
  if ( 'owner' == $post->post_type ) {
     
    $first_name = get_post_meta( $post_id, 'wpcf-first-name', true );
    $last_name = get_post_meta( $post_id, 'wpcf-last-name', true );
     
    $new_title = $first_name . " " . $last_name;
    $new_title = sanitize_text_field( $new_title );
    $new_slug = sanitize_title( $new_title );
     
    $args = array(
      'ID'          =>   $post_id,
      'post_title'  =>   $new_title,
      'post_name'   =>   $new_slug
    );
    wp_update_post( $args );
  }  
}
add_action( 'save_post', 'tssupp_autogenerate_title', 10, 2 );

Relevant Documentation:

0% of people find this useful.

This support ticket is created vor 6 Jahren, 9 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Dieses Thema enthält 6 Antworten, hat 2 Stimmen.

Zuletzt aktualisiert von larryB-3 vor 6 Jahren, 9 Monaten.

Assistiert von: Nigel.

Author
Artikel
#609954
Screen Shot 2018-01-25 at 2.22.20 PM.jpg
Screen Shot 2018-01-25 at 2.23.01 PM.jpg
Screen Shot 2018-01-25 at 2.23.20 PM.jpg

I am trying to:

I read several posts about this, https://toolset.com/forums/topic/trying-to-create-post-title-from-2-custom-fields/ and https://toolset.com/forums/topic/post-link-from-custom-fields/ but was unable to succesfully create a post title for my CPT of Owners by using the custom fields Owner-First-Name and Owner-Last-Name

Here's the code snippet I used:

add_filter( 'wp_insert_post_data' , 'set_owners_title' , '10', 2 );
 
function set_owners_title( $data , $postarr ) {
if ( 'owners' != $data['post_type'] )
return $data;
 
$post_title = $data['post_title'];
 
if ( ! $post_title ) {
 
$owners_first = trim( $_POST['wpcf']['owner-first-name']); 
$owners_last = trim( $_POST['wpcf']['owner-last-name'] );
 
$owners_title = $owners_first . ' ' . $owners_last;
$post_slug = sanitize_title_with_dashes ($owners_title,'','save');
$post_slugsan = sanitize_title($post_slug);
 
$data['post_title'] = $owners_title;
$data['post_name'] = $post_slugsan;
  }
 
return $data;
}
add_filter( 'wp_insert_post_data' , 'set_owners_title' , '99', 2 );
apply_filters( 'set_owners_title', $data, $postarr );
#610087

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi Larry

The code looks a little confused, if I may say so, you are adding the same filter twice and have a spurious apply_filters call which appears to do nothing.

For a better understanding of how WordPress hooks work you might want to check the plugin handbook: https://developer.wordpress.org/plugins/hooks/

Before commenting further, can I check where the posts are being created? In the front-end with a CRED form or the back-end?

#610158

Sorry, these posts are being created on the back-end. I've just unchecked Title in the CPT configuration screen. So when I enter one, it saves with no title.

The code I used was from someone else's similiar issue. I guess it has too many values in it. Can you provide me with the code needed to use the custom fields Owner-First-Name and Owner-Last-Name to successfully create a post title for my CPT of Owners.

#610166

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi Larry

I wrote some code that you can try below, but please note that providing or debugging custom code falls outside of our support policy, so I'm providing as-is, I haven't tested it, but I think it should do what you need, and I think it is fairly straightforward to follow the logic.

/**
 * Auto-generate post title
 */
function tssupp_autogenerate_title( $post_id, $post ){
  
  if ( 'owner' == $post->post_type ) {
	
	$first_name = get_post_meta( $post_id, 'wpcf-first-name', true );
	$last_name = get_post_meta( $post_id, 'wpcf-last-name', true );
	
	$new_title = $first_name . " " . $last_name;
	$new_title = sanitize_text_field( $new_title );
	$new_slug = sanitize_title( $new_title );
	
	$args = (
	  'ID'			=>	$post_id,
	  'post_title'	=>	$new_title,
	  'post_name'	=>	$new_slug
	);
	wp_update_post( $args );
  }  
}
add_action( 'save_post', 'tssupp_autogenerate_title', 10, 2 );
#610172

Thanks so much Nigel! I know you can't assist in debugging the code, but maybe someone else will see this and can assist.

I got this error when implementing your code.

The code snippet you are trying to save produced a fatal error on line 16:

syntax error, unexpected '=>' (T_DOUBLE_ARROW)

#610185

Nigel
Supporter

Sprachen: Englisch (English ) Spanisch (Español )

Zeitzone: Europe/London (GMT+00:00)

Hi Larry

Yup, I didn't test it 😉

Line 15 needs editing to:

    $args = array(

Hopefully that will do it.

#610192

Thank you!