Support

Account

Home Forums Backend Issues (wp-admin) Auto populate post title with ACF Taxonomy Field

Solved

Auto populate post title with ACF Taxonomy Field

  • Good afternoon!

    I have been trying to get a piece of code that will allow me to populate my title (and permalink) with an ACF field. I found the code (pasted below) but the field I’m trying to use is a taxonomy and it prints the ID of the taxonomy instead of the name, which is what I need. Any help would be greatly appreciated!

    Edited to add: Currently using ACF Pro V. 5.5.12

    function create_title( $post_id ) {
        // Set variables
        $opposition = get_field( 'ministry_name', $post_id );
    
        // Set post title and permalink
        $post_title = $opposition;
        $post_name = $opposition;
    
        // update the post, which calls save_post again
        wp_update_post( array(
            'ID'            => $post_id,
            'post_title'    => $post_title,
            'post_name'     => $post_name,
            )
        );
    }
    
    add_action( 'acf/save_post', 'create_title', 20 );
  • Hi @kikadesign

    For single value Taxonomy selection, you can set the return type to “Term Object” and then make use of this code:

    function create_title( $post_id ) {
        // Set variables
        $term = get_field( 'ministry_name', $post_id );
        $name = $term->name;
    
        // Set post title and permalink
        $post_title = $name;
        
    
        // update the post, which calls save_post again
        wp_update_post( array(
            'ID'            => $post_id,
            'post_title'    => $post_title,
            'post_name'     => $post_name,
            )
        );
    }
    
    add_action( 'acf/save_post', 'create_title', 20 );
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Auto populate post title with ACF Taxonomy Field’ is closed to new replies.