I have stumbled upon a brilliant piece of code that just fits my needs on this thread : Using custom field as custom post title by @chrisguitarguy and I was hoping there might be a way to tweak it to use several custom fields' input to populate a CPT's title.
For example, to be sure there will never be any case of twice the same title being generated I would like to use a custom field named dlc_media_photos_titre
followed by another named dlc_media_photos_date
, this way I should obtain a title in this format : Photo gallery name 20210921
.
The date format doesn't really matter, it is just a security to include it in the title.
Thanks in advance (and excuse me if my post is not usual, it is my first one on StackExchange/Overflow).
edit Yes I will add the code i am using next time, it's so obvious ! Thanks a lot for your help
edit 30/06/2022 I have an addition to bring : @Michelle's code works perfectly, but in case you want to combine the value of a Taxonomy with custom fields, this code will work :
add_filter( 'the_title', 'custom_post_title', 10, 2 );
function custom_post_title( $title, $post_id ) {
$post_type = get_post_type( $post_id );
if ( $post_type == 'my-post-type' ) {
$taxonomy = get_the_terms( $post_id, 'my-taxonomy-name' );
$taxonomy = $taxonomy[0]->name;
$custom_field = get_post_meta( $post_id, 'my-custom-field', true );
$title = $taxonomy . ' - ' . $custom_field;
}
return $title;
}