Use meta box value in CPT as post title

<?php
add_action( 'save_post', 'post_updated' );

function post_updated( $post_id ) {

    // verify post is not a revision & not an autosave
    if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
        // set the new post title
        $post['ID'] = $post_id;
        $post['post_title'] = get_post_meta($post_id, 'ecpt_name', true);

        // update the post, removing the action to prevent an infinite loop
        remove_action( 'save_post', 'post_updated' );
        wp_update_post($post);
        add_action( 'save_post', 'post_updated' );
    }

}
?>

Leave a Comment