Get the current post in Wordpress

A quick and easy way to get the current post in WordPress, works pretty much anywhere and doesn’t need to be used in the loop.

WordPress provides a nice build in function for getting the current post, however it is important to ensure that the user is viewing a single post when using it.

Here is the snippet with the single post check:

if(is_single()) {
    $post = get_queried_object();

That’s it, nice and simple. The post is an object, so you can do things like ‘$post->ID’ to get the ID of the post.