I've read here and other places about setting a post's title based on it's metadata. Unfortunately I'm unable to get it working and I'm hoping someone else can point me in the proper direction or explain where I'm wrong. Here's my code:
function setup_match_title( $data, $postarr ) {
if ( $data['post_type'] == 'matches' && $data['post_title'] == '' ) :
$a = get_the_title( get_post_meta( $postarr['ID'], 'away_name', true ) );
$h = get_the_title( get_post_meta( $postarr['ID'], 'home_name', true ) );
$data['post_title'] = $a . ' vs ' . $h;
endif;
return $data;
}
add_filter( 'wp_insert_post_data', 'setup_match_title', 99, 2 );
The home and away team metaboxes are select boxes populated with a list of "teams" post_type that stores the team's post_id.
When I publish a new "match", my post title reads "Auto draft vs Auto draft". My guess is since nothing is in the database when get_post_meta()
runs, it returns Auto Draft. I believe that because if I go back into the post, empty the title (so it passes the initial if statement) and Update it, the proper title is set.
What am I doing wrong? Is there any way to get the value of a select box at runtime using get_post_meta()
?