How to Get the Post ID in WordPress?
source link: https://code.tutsplus.com/tutorials/how-to-get-the-post-id-in-wordpress--cms-93241
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to Get the Post ID in WordPress?
Read Time: 3 mins
Different posts and pages in WordPress are identified uniquely by their IDs. Once you know the ID of a post, you can get other relevant information like its title or publication date etc.
In this post, I will show you to two methods to get the post ID in WordPress.
Using the get_the_ID()
Function to Get Post ID
You can easily get the ID of the current post in the WordPress loop using the get_the_ID()
function. This function returns the ID of the current post in the WordPress loop or returns false
if the $post
variable is not set.
You might be wondering what do I mean by WordPress loop. The WordPress loop is one of the techniques used to display the contents of a post within template files.
If you want to learn basic PHP coding for WordPress, check out our free course.
Learn PHP for WordPress | FREE COURSE
What is PHP, and how can you use it for WordPress programming? Discover the answers to those questions and more in our free course, Learn PHP for WordPress.
You can also find many tutorials on coding WordPress here on Envato Tuts+.
Here is an example that gets the ID of the current post in WordPress:
<?php while ( have_posts() ) { the_post(); do_action( 'storefront_single_post_before' ); echo "<h2>The Post ID: " .get_the_ID(). "</h2>" ; get_template_part( 'content' , 'single' ); do_action( 'storefront_single_post_after' ); } ?> |
The above snippet is from the single.php file in Storefront theme. I modified it to echo the post ID before the post content.
Lets say that you have a FAQs page inside the theme with file name page-faqs.php. You will be able to use the same get_the_ID()
function to get the ID of the page.
I would also like to add that you can generally get the post ID using this function even outside the loop. However, it might not always be accurate.
Using get_queried_object_id()
Function to Get the Page ID
Another function that you can use to get the ID of any queried object such as a post or page ID in WordPress is the get_queried_object_id()
function. This function will give you the ID of the current post or page even outside the loop.
This function is especially useful on pages such as the blog homepage. In this case, using the function get_the_ID()
will return the ID of the first blog post in the loop.
For my website, I have set the blog homepage to be /blog/
. Now, the ID of this page is 134. The ID of the first post that is listed on the blog page is 192. If I call the function get_the_ID()
on this page, it will return 192. What I actually want here is the value 134.
Try adding the following lines on your blog homepage and you will see different IDs being output by these functions.
<?php // The post ID: 192 echo "<h2>The post ID: " .get_the_ID(). "</h2>" ; // The Queried Object ID: 134 echo "<h2>The Queried Object ID: " .get_queried_object_id(). "</h2>" ; ?> |
Putting the above code after the last post in the loop would have resulted in the output of the get_the_ID()
function being the ID of the last post. The function get_queried_object_id()
would still return the proper page ID.
If you use this function on the category archive page, it will return the category ID instead because there is no specific category page.
Final Thoughts
In this quick tip, we learned about two different functions that you can use to get the ID of current post or page in WordPress. You can also get the post ID by using the global $post
object but that will not always be reliable as someone might have tweaked its value.
I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python.
I usually spend my free time either working on some side projects or traveling around.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK