WordPress get_the_title() Function

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().
<?php
echo esc_html( get_the_title() );
| Call | What 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
get_the_title( int|WP_Post $post = 0 ): string
Parameters
| Parameter | Description |
|---|
$post | int|WP_Post. The post ID or object. Default is 0, which means the current post. |
Return Value
| Return Value | Description |
|---|
| string | The 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().
<h1><?php echo esc_html( get_the_title() ); ?></h1>
How It Works
get_the_title() returns the current title.esc_html() makes it safe for page text.echo prints the result.
Example 2 — Get the title of a given post
Pass an ID to read the title of any post.
<?php
$title = get_the_title( 42 );
echo 'Read next: ' . esc_html( $title );
Read next: My First WordPress Theme
How It Works
42 is a post ID.- The title comes back as a string.
- We join it to some text and print it.
Example 3 — Use it in an image alt text
Use the title inside an attribute with esc_attr().
<img src="<?php the_post_thumbnail_url( 'medium' ); ?>"
alt="<?php echo esc_attr( get_the_title() ); ?>">
<img src="https://example.com/wp-content/uploads/2026/11/cover-300x200.jpg"
alt="Hello World">How It Works
- Inside an attribute you must use
esc_attr(). get_the_title() is perfect here because it returns, not prints.- The image alt text now matches the post title.
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
Developer, cloud engineer, and technical writer
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