Filter Hook since 0.71
the_title
Filters the post title
Description
the_title changes how a title is displayed, not the value in the database. Guard with post ID so menus and admin lists are not rewritten by accident.
When it runs
Whenever the_title() or get_the_title() runs with the filter on — loops, menus, and some admin screens.
Signature
apply_filters( 'the_title', string $title, int $id );Parameters
$titlestring — The post title.$idint — Post ID.
Examples
Basic
add_filter( 'the_title', function( $title, $id ) {
if ( get_post_type( $id ) === 'book' ) {
return 'Book: ' . $title;
}
return $title;
}, 10, 2 );Prefix titles for one post type only.
Real case
add_filter( 'the_title', function( $title, $id ) {
if ( ! is_admin() && empty( $title ) ) {
return __( '(Untitled)', 'my-textdomain' );
}
return $title;
}, 10, 2 );Show a fallback label instead of a blank title on the front-end.
Common Use Cases
- Prefix/suffix titles by post type
- Truncate long titles
- Add icons or labels to titles
- Translate titles dynamically
Common mistakes
- Altering titles in nav menus because the filter also runs there — check in_the_loop() or post type
Related hooks
FAQ
Why did the_title also change my menu item labels?
The filter runs anywhere get_the_title()/the_title() is called, including some menu-label code paths. Check the post type or scope the condition more tightly.
Source: wp-includes/post-template.php