WordPress get_the_title() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns a title

Definition

In WordPress, get_the_title() returns the title of a post. It does not print, so you can use it in a variable, in a link, or inside an attribute.

The print version is the_title(). Use get_the_title() when you need to work with the text first.

Syntax

A few ways to call get_the_title().

WordPress
<?php
echo esc_html( get_the_title() );
CallWhat it does
get_the_title()Title of the current post.
get_the_title( 12 )Title of the post with ID 12.
get_the_title( $post )Title of a post object.

It returns the title. When you print it, wrap it in esc_html() to keep the page safe.

Full declaration
WordPress
get_the_title( int|WP_Post $post = 0 ): string

Parameters

ParameterDescription
$postint|WP_Post. The post ID or object. Default is 0, which means the current post.

Return Value

Return ValueDescription
stringThe title. It is an empty string if the post has no title.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Print a safe title

Return the title, then print it with esc_html().

WordPress
<h1><?php echo esc_html( get_the_title() ); ?></h1>

How It Works

  1. get_the_title() returns the current title.
  2. esc_html() makes it safe for page text.
  3. echo prints the result.

Example 2 — Get the title of a given post

Pass an ID to read the title of any post.

WordPress
<?php
$title = get_the_title( 42 );
echo 'Read next: ' . esc_html( $title );

How It Works

  1. 42 is a post ID.
  2. The title comes back as a string.
  3. We join it to some text and print it.

Frequently Asked Questions

It returns the title of a post as a string.
the_title() prints the title. get_the_title() returns it, so you can change it or use it in code.
Yes. Use esc_html() in page text and esc_attr() inside attributes.
WordPress adds Protected: to the title of password-protected posts, and Private: to private posts.
Yes. Pass the post ID, for example get_the_title( 42 ).

Browse all post functions

Open the functions index for simple guides with examples and FAQs.

Post Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful