Thursday, October 20, 2022

How to Get the Post ID in WordPress?

How to Get the Post ID in WordPress?

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.

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 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.

    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.


    No comments:

    Post a Comment