wppaste
WordPress

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.

Retrieves the post title.

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?

Both prefixes sit inside an 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?

The default $post value is 0, which get_post() resolves against the global $post. If you call the function outside The Loop or before $post is populated, get_post() can return null and post_title falls back to an empty string via the null coalescing operator in the source.

Why does the title I get back contain curly quotes or entities I never typed?

The value passes through the 'the_title' filter before it is returned, and WordPress hooks wptexturize() onto that filter by default, so straight quotes and dashes get converted.

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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
20–44

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 60.

Plugin surface
3 hooks

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.

Called by
49

49 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always20–29get_post(), is_admin(), apply_filters()
!is_admin()38–44get_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:

  1. apply_filters( protected_title_format )filterline 141 (+23 into the body)

    Filters the text prepended to the post title for protected posts.

  2. apply_filters( private_title_format )filterline 160 (+42 into the body)

    Filters the text prepended to the post title of private posts.

  3. apply_filters( the_title )filterline 174 (+56 into the body)

    Filters the post title.

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

Show all 49

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.