How to Get Post Title in WordPress (2 Examples!)

2.5 from 5
from 14 user
(Rate this post)
How to Get Post Title in WordPress (2 Examples!)

get_the_title is the core function responsible for retrieving Post Title in WordPress. We'll try this function inside loops and with a specific post ID. This tutorial can be the only page you need to visit to learn how to print the title of a post when developing WordPress.

Introducing get_the_title function: Responsible for retrieving Post Title in WordPress

Each WordPress Post has a Title, and You can retrieve this title for each post easily using the get_the_title function (read more on WordPress official resources about get_the_title).

It is optional for developers to specify ID (integer) or post object (WP_Post object) to retrieve a specific post’s title with the get_the_title function.

When you pass a specific ID (integer) or post object (WP_Post) to this function, it will retrieve the title of the specified post.

get_the_title function syntax in WordPress:

get_the_title(int|WP_Post $post)

get_the_title is not that tricky to use, but we’ll try this function in a live WordPress environment to see what happens.

How to get the title of a specific post using Post ID

When you pass an id as an argument to the get_the_title function, it will return the post title (if available).

In this example, we tried printing the post title of a post with an id equal to 1:

$postId = 1;
echo get_the_title($postId);

How to get the title of the current post

If tIf the developer does not specify the post ID or Object, the WordPress get_the_title function will retrieve the current queried object title.

There is a global $post variable in WordPress; When you use the get_the_title function with no argument, WordPress will check this variable for the ID.

Note: you can change the global $post (offen inside loops). then it is possible to run into conflicts when you did not reset the global $post back to it’s WordPress default after a loop.

use wp_reset_postdata function to reset $post object back to it’s original value after custom loops!

$posts = new WP_Query(array('post_type' => 'post')); //retrieve posts
if($posts->have_posts() ) {
    while($posts->have_posts() ) {
        echo get_the_title();

If you want to try these codes, I recommend creating a plugin to organize your codes.

Creating a WordPress plugin is easy and fast.

You can read my Create a WordPress Hello World Plugin in 3 Steps tutorial for a start.

What do you think about "How to Get Post Title in WordPress (2 Examples!)"?

Your rating submit was successfull.

Your rating submit was not successfull.