Thursday, October 20, 2022
HomeWeb DevelopmentFind out how to Get the Submit ID in WordPress?

Find out how to Get the Submit ID in WordPress?


Completely different posts and pages in WordPress are recognized uniquely by their IDs. As soon as you already know the ID of a put up, you may get different related info like its title or publication date and so forth.

On this put up, I’ll present you to 2 strategies to get the put up ID in WordPress.

Utilizing the get_the_ID() Perform to Get Submit ID

You may simply get the ID of the present put up within the WordPress loop utilizing the get_the_ID() perform. This perform returns the ID of the present put up within the WordPress loop or returns false if the $put up variable is just not set.

You is perhaps questioning what do I imply by WordPress loop. The WordPress loop is among the methods used to show the contents of a put up inside template recordsdata.

If you wish to study primary PHP coding for WordPress, try our free course.

You can too discover many tutorials on coding WordPress right here on Envato Tuts+.

Right here is an instance that will get the ID of the present put up in WordPress:

The above snippet is from the single.php file in Storefront theme. I modified it to echo the put up ID earlier than the put up content material.

Let’s imagine that you’ve got a FAQs web page contained in the theme with file identify page-faqs.php. It is possible for you to to make use of the identical get_the_ID() perform to get the ID of the web page.

I might additionally like so as to add which you could typically get the put up ID utilizing this perform even outdoors the loop. Nevertheless, it won’t all the time be correct.

Utilizing get_queried_object_id() Perform to Get the  Web page ID

One other perform that you need to use to get the ID of any queried object reminiscent of a put up or web page ID in WordPress is the get_queried_object_id() perform. This perform gives you the ID of the present put up or web page even outdoors the loop.

This perform is very helpful on pages such because the weblog homepage. On this case, utilizing the perform get_the_ID() will return the ID of the primary weblog put up within the loop.

For my web site, I’ve set the weblog homepage to be /weblog/. Now, the ID of this web page is 134. The ID of the primary put up that’s listed on the weblog web page is 192. If I name the perform get_the_ID() on this web page, it can return 192. What I truly need right here is the worth 134.

Attempt including the next strains in your weblog homepage and you will notice completely different IDs being output by these capabilities.

Placing the above code after the final put up within the loop would have resulted within the output of the get_the_ID() perform being the ID of the final put up. The perform get_queried_object_id() would nonetheless return the correct web page ID.

In case you use this perform on the class archive web page, it can return the class ID as a substitute as a result of there isn’t any particular class web page.

Remaining Ideas

On this fast tip, we realized about two completely different capabilities that you need to use to get the ID of present put up or web page in WordPress. You can too get the put up ID by utilizing the worldwide $put up object however that won’t all the time be dependable as somebody may need tweaked its worth.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments