Web development · 30 Aug 2014 · 3 min read

How to get the current page or post ID in WordPress

When you customise a WordPress theme you often need to know which page or post is being shown, to load a script on one page, hide a block on another, or change a query. There are two functions for this, and which one is right depends on where your code runs.

Inside the loop: get_the_ID()

In a template file (page.php, single.php, a template part), inside the loop, use get_the_ID():

<?php
while ( have_posts() ) :
    the_post();
    $current_id = get_the_ID();

    if ( 42 === $current_id ) {
        // Something only for the post or page with ID 42.
    }
endwhile;

get_the_ID() returns the ID of the current post in the loop, and false if there isn’t one. In a custom WP_Query loop it returns the post you are looping over, which is what you want there.

Anywhere else: get_queried_object_id()

In functions.php, a hook, a plugin, the header or the sidebar, there may be no loop, or the loop may be a different one. Use get_queried_object_id(). It returns the ID of the thing WordPress was asked for: the page, the post, or the category or tag on an archive.

<?php
add_action( 'wp_enqueue_scripts', function () {
    if ( 42 === get_queried_object_id() ) {
        wp_enqueue_script(
            'pricing-calculator',
            get_theme_file_uri( 'js/pricing-calculator.js' ),
            array(),
            '1.0',
            true
        );
    }
} );

It returns 0 when there is nothing specific (the search results page, for example). wp_enqueue_scripts runs after the main query, so the value is ready there. On very early hooks such as init, it isn’t set yet.

The posts page gotcha

If Settings → Reading sets a page as the “Posts page”, that page shows your latest posts. On it:

  • get_the_ID() returns the ID of the first post in the list, not the page.
  • get_queried_object_id() returns the page’s ID.

To check for that page specifically, compare with the setting:

<?php
if ( is_home() && get_queried_object_id() === (int) get_option( 'page_for_posts' ) ) {
    // We're on the posts page.
}

Often you don’t need the ID at all

Hard-coding IDs breaks when content is moved between a staging site and production, because the IDs differ. WordPress’s conditional tags accept an ID, a slug or a title, and read better:

<?php
if ( is_page( 'contact-us' ) ) {
    // The page with slug "contact-us".
}

if ( is_single( array( 12, 'launch-announcement' ) ) ) {
    // Post 12, or the post with that slug.
}

if ( is_page_template( 'templates/landing.php' ) ) {
    // Any page using the landing page template.
}

Using it in CSS instead

If all you need is a style change on one page, you may not need PHP. body_class() adds the ID to the <body> tag (page-id-42 or postid-42), so:

.page-id-42 .site-header {
  background: #062a40;
}

Block themes

In a block theme the templates are HTML, not PHP, so there is no template file to put a check in. Put the logic in a plugin or functions.php using a hook and get_queried_object_id(), or use a block’s own visibility settings where one exists.

Quick reference

Where your code runsUse
Inside the main loop or a WP_Query loopget_the_ID()
Hooks, functions.php, header, sidebar, pluginsget_queried_object_id()
Checking for one specific pageis_page( 'slug' )
The posts pageget_option( 'page_for_posts' )
Styling one pageThe page-id-N body class

Customising a WordPress theme or plugin?

We build custom WordPress themes and plugins in Shimla, and take over sites that have outgrown the last developer's code.

See our WordPress and web work

Peak performance
& connectivity

31.0785°N 77.1800°E · Kasumpti, Shimla · 2,205 m