Saltar navegación

[Resuelto] Auto-generate Post Title from custom field values

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 hace 6 años, 11 meses. 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
- 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)

Etiquetado: 

Este tema contiene 6 respuestas, tiene 2 mensajes.

Última actualización por larryB-3 hace 6 años, 11 meses.

Asistido por: Nigel.

Autor
Mensajes
#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

Idiomas: Inglés (English ) Español (Español )

Zona horaria: 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

Idiomas: Inglés (English ) Español (Español )

Zona horaria: 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

Idiomas: Inglés (English ) Español (Español )

Zona horaria: 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!