Skip Navigation

[Resolved] Autogenerate title from custom field and custom taxonomy name

This support ticket is created 4 years, 10 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 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 1 reply, has 1 voice.

Last updated by anthonyD-3 4 years, 10 months ago.

Assisted by: Shane.

Author
Posts
#1464377

Tell us what you are trying to do?
Autogenerate title from custom field and custom taxonomy name

Is there any documentation that you are following?
I current using a toolset snippet that allows me to autogenerate a post title from two custom fields; however, I am unable to get this working with Custom Field + Custom Category Name

Is there a similar example that we can see?
Not at this time

What is the link to your site? This is a private site

This code works fine

// Neutral Names Autogenerate Post Title

function njea_autogenerate_neutrals_title( $post_id, $post ){

  if ( 'neutral' == $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
    );

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

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

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

  }
}
add_action( 'save_post', 'njea_autogenerate_neutrals_title', 30, 2 );
//

This code does not yet work.

// Evaluation Autogenerate Post Title

function njea_autogenerate_evaluation_title( $post_id, $post ){

  if ( 'evaluation' == $post->post_type ) {

    $first_name = get_post_meta( $post_id, 'wpcf-local-name', true );
    $last_name = get_the_terms( $post_id ) [0]->name;

    $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
    );

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

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

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

  }
}
add_action( 'save_post', 'njea_autogenerate_evaluation_title', 30, 2 );
//
#1464735

My issue is resolved now. Thank you!