With this in your theme’s functions.php you can show a custom field with a key “title” as the title (if there is no title):
add_filter( 'the_title', 'meta_title', 10, 2 );
function meta_title( $title, $id ) {
if ( trim( $title ) == '' ) {
$meta_title = get_post_meta( $id, 'title', true );
if ( $meta_title ) {
$title = $meta_title;
}
}
return $title;
}
Another (less reliable) way is to use a regular expression to try and get all the characters of the post content before the first “.?!” followed by a space.
add_filter( 'the_title', 'first_sentence', 10, 2 );
function first_sentence( $title, $id ) {
if ( trim( $title ) == '' ) {
$post = get_post( $id );
if ( isset( $post->post_content ) && $post->post_content ) {
if ( preg_match( '/(.*?[?!.](?=\s|$)).*/', $post->post_content ) ) {
$title = preg_replace( '/(.*?[?!.](?=\s|$)).*/', '\\1', $post->post_content );
}
}
}
return $title;
}
Don’t use both code samples at the same time.