Skip Navigation

[Resolved] add auto generate post title from fields

This thread is resolved. Here is a description of the problem and solution.

Problem:
add auto generate post title from fields

Solution:
You can use the standard wordpress action "save_post" hook and the save_post action must be used with a priority of at least 30 when used in conjunction with post types registered with Types, or inconsistencies in the updated data may be experienced.

You can find the proposed solution, in this case with the following reply:
https://toolset.com/forums/topic/add-auto-generate-post-title-from-fields/#post-1213248

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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

Last updated by TorstenR3514 5 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#1213065

Tell us what you are trying to do?
trying to generate a slug like "year-month-title" for bildungsangebot type

Is there any documentation that you are following?
https://toolset.com/forums/topic/create-post-title-for-cpt-from-two-custom-fields/
or is there an easier way to add a date to the slug, so even equal titles are unique?

Tried the following code in my child theme function, but i get an error. Warning: Cannot modify header information - headers already sent by (output started at /www/htdocs/w012252b/seiten/dev-client.reinecke.design/cms/wp-content/themes/crocal-child/functions.php:22) in /www/htdocs/w012252b/seiten/dev-client.reinecke.design/cms/wp-admin/includes/misc.php on line 1198

function sha_autogenerate_title( $post_id, $post ){
    
  if ( 'bildungsangebot' == $post->post_type ) {
      
    $start_datum = get_post_meta( $post_id, 'wpcf-kurs-start', true );
    $kurs_titel = get_post_meta( $post_id, 'wpcf-kurs-titel', true );
      
    $new_title = $start_datum . " " . $kurs_titel;
    $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', 'sha_autogenerate_title', 10, 2 ); 

Is there a similar example that we can see?

What is the link to your site?
hidden link
HTTP protection: toolset:562VFHjfv

#1213144

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Well - We offer detailed Doc that should help you out here:
=> https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

The save_post action must be used with a priority of at least 30 when used in conjunction with post types registered with Types, or inconsistencies in the updated data may be experienced.

For example:

function sha_autogenerate_title( $post_id, $post ){
     
  if ( 'bildungsangebot' == $post->post_type ) {
       
    $start_datum = get_post_meta( $post_id, 'wpcf-kurs-start', true );
    $kurs_titel = get_post_meta( $post_id, 'wpcf-kurs-titel', true );
       
    $new_title = $start_datum . " " . $kurs_titel;
    $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', 'sha_autogenerate_title', 30, 2 );
#1213239

Thx for that link, but the way is the right one? Or?
Even if I'll maybe have to get a higher priority than '30'.

Thx Torsten

#1213248

Minesh
Supporter

Languages: English (English )

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

We need minimum priority 30 you can set 31,40 etc.. but 30 is enough.

Please try to use the following example code where I changed the priority:

function sha_autogenerate_title( $post_id, $post ){
      
  if ( 'bildungsangebot' == $post->post_type ) {
        
    $start_datum = get_post_meta( $post_id, 'wpcf-kurs-start', true );
    $kurs_titel = get_post_meta( $post_id, 'wpcf-kurs-titel', true );
        
    $new_title = $start_datum . " " . $kurs_titel;
    $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
    );

       // unhook this function so it doesn't loop infinitely
        remove_action('save_post', 'sha_autogenerate_title',30,2);

        // update the post, which calls save_post again
        wp_update_post( $args );

        // re-hook this function
        add_action('save_post', 'sha_autogenerate_title',30,2);

    
  }  
}
add_action( 'save_post', 'sha_autogenerate_title', 30, 2 );
#1213255

My issue is resolved now. Thank you!