get_the_title()WP 0.71

Gets the title of the post for further use in php. You can specify the post whose title you want to get.

The function can be used inside a WordPress Loop without specifying a parameter, then the title of the current post will be returned. Or pass a post ID in the first parameter and the function will return the title of this specified post.

If the post is protected or private and you are on front-end, then "Protected" or "Private" will be displayed before the post title.

There is NO get_post_title() function in WordPress, use this function instead.

When you need to add post title to an HTML tag attribute, use the_title_attribute() function instead of the_title() or get_the_title().

<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>

Value of get_the_title() recommended to be cleaned before displaying because it displays all HTML tags as is, and it is not safe. There are two ways to clean it:

  • Full cleaning. Prevents the use of any HTML tags:

    echo esc_html( get_the_title() );
  • Partial cleaning. Allows you to leave the basic HTML tags (performance in this case suffers):
    echo wp_kses_post( get_the_title() );
Used By: the_title()
1 time — 0.000346 sec (fast) | 50000 times — 0.68 sec (very fast) | PHP 7.1.0, WP 4.7

Return

String. Post title.

Usage

get_the_title( $post );
$post(int|WP_Post)
Post ID or WP_Post object.
Default: global $post

Examples

0

#1 Print the current post title

echo get_the_title();

// or in H1 tag
echo '<h1>'. esc_html( get_the_title() ) .'</h1>';
0

#2 Print the title of post 25

echo get_the_title( 25 );

// or pass parameter as object
$the_post = get_post( 25 );
echo get_the_title( $the_post );
0

#3 Should the output be escaped with back slash

It depends on the context in which the function is used. As a rule, you don't need to escape it, WP does it itself. In particular, the following functions apply to the the_title hook:

Screening may be necessary when the header is rendered in a tag attribute:

<input type="text" value="<?= esc_attr( get_the_title() ) ?>">

Some general screening function can be put on the hook:

add_filter( 'the_title', 'my_escape_title' );

function my_escape_title( $title ){
	return esc_html( $title );
}

Changelog

Since 0.71 Introduced.

get_the_title() code WP 6.5.2

function get_the_title( $post = 0 ) {
	$post = get_post( $post );

	$post_title = isset( $post->post_title ) ? $post->post_title : '';
	$post_id    = isset( $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 );
}