Skip to content Skip to sidebar Skip to footer

WordPress get specific page or post ID

Reading Time: < 1 minute

How can I get specific page or post ID to any page/plugin/widget in the WordPress.You don’t have a write sql codes.Simply call to WordPress core functions only.If you want to get page or post ID to any page like plugin page, template page, inside functions, outside functions no matter you can use this.
My another post is described how to get specific page or post content.

Okay.We’ll see how to get specific page or post ID.

global $wp_query;
$page_id = $wp_query->get_queried_object_id();
// USAGE
if($page_id==2) {
    // Contents goes here
}

Get Current page or post ID

If you are inside a loop, then get Current page or post ID as follows.

$page_id = $post->ID;

If you are outside a loop, then get Current page or post ID as follows.(This is the another method to get current page ID).

$page_id = $wp_query->post->ID;