I have set up a custom post type without title support and have added a code for the title to be automatically populated from custom fields. This works great when creating or updating a post on the edit post page. The problem is, when I update the post via the quick edit menu, the title goes back to (no title).
Would anyone know what I could change or add in my code so that the title remains when using quick edit?
Here is my code:
function kida_post_updated_func( $post_id ) {
if ( wp_is_post_revision( $post_id ) || get_post_type($post_id) != 'plot_detail')return;
remove_action( 'post_updated', 'kida_post_updated_func' );
$given = get_post_meta($id, 'wpcf-given', true);
$givenslug = "given";
$nickname = get_post_meta($id, 'wpcf-nickname', true);
$nicknameslug = "nickname";
$middle = get_post_meta($id, 'wpcf-middle', true);
$middleslug ="middle";
$maiden = get_post_meta($id, 'wpcf-maiden-name', true);
$maidenslug ="maiden-name";
$family = get_post_meta($id, 'wpcf-family', true);
$familyslug = "family";
if(isset($_POST['wpcf'][$givenslug]) && !empty($_POST['wpcf'][$givenslug])){
$a = $_POST['wpcf'][$givenslug];
}
if(isset($_POST['wpcf'][$nicknameslug]) && !empty($_POST['wpcf'][$nicknameslug])){
$b = $_POST['wpcf'][$nicknameslug];
$bb = '"';
}
if(isset($_POST['wpcf'][$middleslug]) && !empty($_POST['wpcf'][$middleslug])){
$c = $_POST['wpcf'][$middleslug];
}
if(isset($_POST['wpcf'][$maidenslug]) && !empty($_POST['wpcf'][$maidenslug])){
$d = $_POST['wpcf'][$maidenslug];
$dd = '(';
$de = ')';
}
if(isset($_POST['wpcf'][$familyslug]) && !empty($_POST['wpcf'][$familyslug])){
$e = $_POST['wpcf'][$familyslug];
}
$v = $a . ' ' . $bb . $b . $bb . ' ' . $c . ' ' . $dd . $d . $de . ' ' . $e;
$my_args = array(
'ID' => $post_id,
'post_title' => $v,
'post_name' => sanitize_title($v),
);
// update the post, which calls save_post again
$res = wp_update_post( $my_args, true );
add_action( 'post_updated', 'kida_post_updated_func' );
}
add_action( 'post_updated', 'kida_post_updated_func' );