Advertisement
  1. Code
  2. WordPress

How to Get the Post ID in WordPress

Scroll to top
Read Time: 3 min

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.

In this post, I will show you two methods to get the post ID in WordPress.

Using the get_the_ID() Function to Get the 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 I mean by the 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.

FREE
2.4 Hours

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:

    The above snippet is from the single.php file in the Storefront theme. I modified it to echo the post ID before the post content.

    Let's say that you have a FAQs page inside the theme with the 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 the 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.

    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 get_queried_object_id() function 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 the 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.

    Advertisement
    Did you find this post useful?
    Want a weekly email summary?
    Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
    Advertisement
    Looking for something to help kick start your next project?
    Envato Market has a range of items for sale to help get you started.