<?php
function generate_title( $post_id, $post ){
if ( 'slug' == $post->post_type ) {
$field1 = get_post_meta( $post_id, 'key', true );
$field2 = get_post_meta( $post_id, 'key', true );
$title_fileds = $field1 . " " . $field2;
$new_title = sanitize_text_field( $title_fileds );
$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', 'generate_title',30,2);
// update the post, which calls save_post again
wp_update_post( $args );
// re-hook this function
add_action('save_post', 'generate_title',30,2);
}
}
add_action( 'save_post', 'generate_title', 30, 2 );
?>