get_the_title( int|WP_Post $post = 0 ): string
- Since
- 0.71
- Source
wp-includes/post-template.php:118
Fetches a post's title, prepending "Protected:" or "Private:" text when appropriate. The prefix is only added on the front end, since the check is skipped whenever is_admin() returns true. Accepts a post ID or WP_Post object and falls back to the global $post when omitted. Use the_title() instead when you want a template tag that echoes the already filtered title directly inside a loop.
Description
If the post is protected and the visitor is not an admin, then "Protected" will be inserted before the post title. If the post is private, then "Private" will be inserted before the post title.
Compatibility
- WordPress
- since 0.71
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$postint|WP_Postoptional- Post ID or WP_Post object. Default is global $post.Default:
0
Return value
string
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Get a post's title by ID outside the loop
Print the title of post 1 without relying on the loop's global $post being set.
$title = get_the_title( 1 );
echo esc_html( $title );Show the 'Private:' prefix on a private post's title
Create a private post and confirm get_the_title() prepends the private label when viewed on the front end.
$private_posts = get_posts(
array(
'post_status' => 'private',
'posts_per_page' => 1,
)
);
if ( ! empty( $private_posts ) ) {
echo esc_html( get_the_title( $private_posts[0] ) );
} else {
echo 'No private post found.';
}The 'Private:' text only appears when is_admin() is false, so it will not show while browsing wp-admin.
Common problems and fixes · 3
- Why doesn't get_the_title() show 'Private:' or 'Protected:' when I test it in wp-admin?
- Why is the returned title an empty string when I call get_the_title() with no argument?
- Why does the title I get back contain curly quotes or entities I never typed?
Why doesn't get_the_title() show 'Private:' or 'Protected:' when I test it in wp-admin?
if ( ! is_admin() ) block in the source, so the check is skipped entirely on any admin screen or admin-side request.Why is the returned title an empty string when I call get_the_title() with no argument?
Why does the title I get back contain curly quotes or entities I never typed?
Alternatives and related functions
the_title- When you want to echo the title directly inside The Loop instead of capturing it in a variable first.
single_post_title- When you need the title for a document tag or an archive/singular page header rather than inline post content.
get_post- When you need the raw, unfiltered post_title property alongside the rest of the post's fields instead of the formatted string get_the_title() returns.
Performance profile
How much work a call to get_the_title() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 20–44
- Plugin surface
- 3 hooks
- Called by
- 49
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 60.
Third-party callbacks on 'protected_title_format', 'private_title_format', 'the_title' run inside this call, and their cost is not bounded by anything here.
49 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_the_title() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 20–29 | get_post(), is_admin(), apply_filters() |
!is_admin() | 38–44 | get_post(), is_admin(), __(), apply_filters(), sprintf(), apply_filters() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 60 instructions, 20–44 executed per call, 6 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 3
3 hooks fire while get_the_title() runs, in this order:
- apply_filters( protected_title_format )filterline 141 (+23 into the body)
Filters the text prepended to the post title for protected posts.
- apply_filters( private_title_format )filterline 160 (+42 into the body)
Filters the text prepended to the post title of private posts.
Uses · 4
- get_post()Retrieves post data given a post ID or post object.
- is_admin()Determines whether the current request is for an administrative interface page.
- __()Retrieves the translation of $text.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 49
- WP_Comments_List_Table::column_response()Outputs the response column.
- WP_Posts_List_Table::get_no_title_excerpt()Gets a trimmed excerpt to display in place of a missing post title.
- WP_REST_Global_Styles_Controller::prepare_item_for_response()Prepare a global styles config output for response.
- WP_REST_Post_Search_Handler::prepare_item()Prepares the search result for a given post ID.
- WP_REST_Posts_Controller::prepare_item_for_response()Prepares a single post output for response.
- WP_REST_Revisions_Controller::prepare_item_for_response()Prepares the revision for the REST response.
- WP_Widget_Recent_Comments::widget()Outputs the content for the current Recent Comments widget instance.
- WP_Widget_Recent_Posts::widget()Outputs the content for the current Recent Posts widget instance.
- Walker_Nav_Menu_Edit::start_el()Start the element output.
- _WP_Editors::wp_link_query()Performs post queries for internal linking.
- _draft_or_post_title()Gets the post title.
- _wp_ajax_menu_quick_search()Prints the appropriate response to a menu quick search.
Show all 49
- block_core_breadcrumbs_get_post_title()Gets a post title with fallback for empty titles.
- comments_popup_link()Displays the link to the comments for the current post ID.
- get_adjacent_image_link()Gets the next or previous image link that has the same post parent.
- get_oembed_response_data()Retrieves the oEmbed response data for a given post.
- get_post_embed_html()Retrieves the embed code for a specific post.
- get_the_privacy_policy_link()Returns the privacy policy link with formatting, when applicable.
- get_the_title_rss()Retrieves the current post title for the feed.
- render_block_core_comments_title()Renders the `core/comments-title` block on the server.
- render_block_core_latest_posts()Renders the `core/latest-posts` block on server.
- render_block_core_post_comments_link()Renders the `core/post-comments-link` block on the server.
- render_block_core_post_featured_image()Renders the `core/post-featured-image` block on the server.
- render_block_core_post_title()Renders the `core/post-title` block on the server.
- render_block_core_read_more()Renders the `core/read-more` block on the server.
- the_title()Displays or retrieves the current post title with optional markup.
- the_title_attribute()Sanitizes the current title when retrieving or displaying.
- trackback_rdf()Generates and displays the RDF for the trackback information of current post.
- twenty_twenty_one_entry_meta_footer()Prints HTML with meta information for the categories, tags and comments.
- twentyfifteen_entry_meta()Prints HTML with meta information for the categories, tags.
- twentyfifteen_excerpt_more()Replaces "[.
- twentyfifteen_post_thumbnail()Displays an optional post thumbnail.
- twentyfourteen_excerpt_more()Replaces "[.
- twentyfourteen_post_thumbnail()Displays an optional post thumbnail.
- twentynineteen_comment_count()Prints HTML with the comment count for the current post.
- twentynineteen_entry_footer()Prints HTML with meta information for the categories, tags and comments.
- twentynineteen_excerpt_more()Replaces "[.
- twentyseventeen_edit_link()Displays an accessibility-friendly link to edit a post or page.
- twentyseventeen_excerpt_more()Replaces "[.
- twentysixteen_entry_meta()Prints HTML with meta information for the categories, tags.
- twentysixteen_excerpt_more()Replaces "[.
- twentythirteen_excerpt_more()Replaces "[.
- twentytwenty_edit_post_link()Filters the edit post link to add an icon and use the post meta structure.
- twentytwenty_read_more_tag()Overwrite default more tag with styling and screen reader markup.
- wp_embed_excerpt_more()Filters the string in the 'more' link displayed after a trimmed excerpt.
- wp_latest_comments_draft_or_post_title()Get the post title.
- wp_prepare_revisions_for_js()Prepare revisions for JavaScript.
- wp_send_note_notification()Sends a single note mention notification email.
- wp_xmlrpc_server::_prepare_comment()Prepares comment data for return in an XML-RPC object.
Source code
function get_the_title( $post = 0 ) { $post = get_post( $post ); $post_title = $post->post_title ?? ''; $post_id = $post->ID ?? 0; if ( ! is_admin() ) { if ( ! empty( $post->post_password ) ) { /* translators: %s: Protected post title. */ $prepend = __( 'Protected: %s' ); /** * Filters the text prepended to the post title for protected posts. * * The filter is only applied on the front end. * * @since 2.8.0 * * @param string $prepend Text displayed before the post title. * Default 'Protected: %s'. * @param WP_Post $post Current post object. */ $protected_title_format = apply_filters( 'protected_title_format', $prepend, $post ); $post_title = sprintf( $protected_title_format, $post_title ); } elseif ( isset( $post->post_status ) && 'private' === $post->post_status ) { /* translators: %s: Private post title. */ $prepend = __( 'Private: %s' ); /** * Filters the text prepended to the post title of private posts. * * The filter is only applied on the front end. * * @since 2.8.0 * * @param string $prepend Text displayed before the post title. * Default 'Private: %s'. * @param WP_Post $post Current post object. */ $private_title_format = apply_filters( 'private_title_format', $prepend, $post ); $post_title = sprintf( $private_title_format, $post_title ); } } /** * Filters the post title. * * @since 0.71 * * @param string $post_title The post title. * @param int $post_id The post ID. */ return apply_filters( 'the_title', $post_title, $post_id );}Changelog
Introduced in 0.71. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/post-template.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.