How to get post title by id in WordPress
Are you looking for the WordPress function to retrieve a certain post title using the id?
I guess you answered YES.
In this post, I will share how to get a post title by id, it is the best way to place a post title outside the while loop.
What you are required to do, is to pass an integer parameter representing the post id to the get_the_title() function.
get_the_title is the WordPress function used to retrieve the title of the post. It receives one optional parameter which can be the ID of the post or the object of the WP_Post class(default object is global $post).
If you want to know the usage of WP_Post class, check the post telling the difference between the_title vs get_the_title
Apart from get_the_title, there is a second solution to output the title outside the loop.
That is the get_postd function.
The code snippet in the index.php file
The code snippet of get_the_title can be written in the index.php file.
It requires an echoing like thisĀ . . . . . .
<?php echo get_the_title($post-id);?>
To retrieve the title using the ID, you should know the id of the post that you want to display its title.
The post ID is the post number in the list of all posts available in the blog.
For example, if the blog has 30 posts and you want to display the title of the sixth post the post ID is six.
Then a code snippet can be expressed as
<?php echo get_the_title(6);?>
The function receives an integer number six(6).
That integer represents the ID of post number six.
Another function to display a post title by ID
get_post can receive an integer that represents the id of a specific post title.
get_the_title uses get_post to get the title of the post hence get_post can be an alternative to pass the ID.
To use get_post, the object that references a function has to be set.
That object is able to retrieve all members of the get_post.
To get the title of the post by ID using this function, you will use the member variable post_title.
This is what I’m talking about.
<php $post_title_no6=get_post(6);?>
<?php echo $post_title_no6->post_title;?>
Therefore, there are two steps to fetch the title of the post using get_post.
- Create the object of get_post
- Output the title by calling the post_title variable
If you have implemented the function correctly, the index.php will display the title of the given post ID.
Why get_the_title is better than get_post?
get_the_title and get_post both can show the title of the post.
But one of them is best to get the post using the post ID.
I can’t give a direct and correct answer to the question.
But get_the_title is the best method to get the title of the post.
This is because you are not required to create an object that will be used to access a member variable post_title.
get_the_title can retrieve the title automatically as long as the post id argument has been passed as a parameter.
Another advantage is that there is no need to know a member variable to display the heading of the post.
Also, get_the_title saves the theme code writing time since one line of code gives a title but not a get_post function.
In fact, I listed two functions that you can pass the id to retrieve the title.
These functions are get_the_title and get_post.
get_the_title is an appropriate function to display the post title using id because it retrieves the title without creating the object.
As I tested the function in some of my templates, it cost only one line of code.
But when you use get_post, you are required to remember two things.
- The id of the post
- The member variable for getting the title.
This is due to the fact that get_post creates an object.
Hence, you need to use the object call the_title member variable.
Why you should write get_the_title outside the loop
When I tried to use the get_the_title function inside the while loop, the output was all posts of the blog.
That is due to the fact that I didn’t pass an integer.
But after passing the id into the function, a single post title repeated multiple times.
Therefore, to fetch the post title by id using get_the_title or get_post the function should be called outside the while loop to avoid a single post title repeating.
Can I get the post id from the post title?
Yes, it is possible to get the page id using the title of the post.
The function which can accomplish the task is get_page_by_title, the function accepts a string parameter.
The string parameter can be the title of the page you want to get the id.
Although, the function can receive two optional parameters.
I have not tried to use the function to get the ID.
There are plenty of usages of get_page_by_title in wordpress get_page_by_title pageĀ .
Wrap-up
To display the post by using the id is recommended if you want the title of the post outside the while loop.
It is good to use the id for retrieving a single specific post title.
So, get_the_title can receive the post id to return the title.
get_the_title is not only the function that is able to get the title of the post but there are other functions that can perform the same task.
get_post returns the object that uses the post_title member variable to retrieve the post title.
Remember to call the function outside the loop when the ID is used to retrieve the post title.