[php] WordPress Get the Page ID outside the loop

I want to get the page ID before starting the loop in WordPress. I am using

$page = get_query_var('page_id');

Apparently, it returns nothing.

I just want to check a page for its ID and add a class to <body> tag based on it.

This question is related to php wordpress

The answer is


If you by any means searched this topic because of the post page (index page alternative when using static front page), then the right answer is this:

if (get_option('show_on_front') == 'page') {
    $page_id = get_option('page_for_posts');
    echo get_the_title($page_id);
}

(taken from Forrst | Echo WordPress "Posts Page" title - Some code from tammyhart)


This is the correct code.

echo $post->ID;

Use below two lines of code to get current page or post ID

global $post;
echo $post->ID;

Use this global $post instead:

global $post;
echo $post->ID;

You can use is_page($page_id) outside the loop to check.


This function get id off a page current.

get_the_ID();

I have done it in the following way and it has worked perfectly for me.

First declared a global variable in the header.php, assigning the ID of the post or page before it changes, since the LOOP assigns it the ID of the last entry shown:

$GLOBALS['pageid] = $wp_query->get_queried_object_id();

And to use anywhere in the template, example in the footer.php:

echo $GLOBALS['pageid];


If you're on a page and this does not work:

$page_object = get_queried_object();
$page_id     = get_queried_object_id();

you can try to build the permalink manually with PHP so you can lookup the post ID:

// get or make permalink
$url = !empty(get_the_permalink()) ? get_the_permalink() : (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$permalink = strtok($url, '?');

// get post_id using url/permalink
$post_id = url_to_postid($url);

// want the post or postmeta? use get_post() or get_post_meta()
$post = get_post($post_id);
$postmeta = get_post_meta($post_id);

It may not catch every possible permalink (especially since I'm stripping out the query string), but you can modify it to fit your use case.


If you are out of the Loop of WordPress you can not use any of the method of wordpress so you must use pure php.

You can use this code. And sure will help you :)

$page_id = @$_GET['page_id'];

if (!is_numeric($page_id)) {
    // Then the uri must be in friendly format aka /my_domain/category/onepage/
    // Try this
    //$path = '/www/public_html/index.php/';
    ///$path = '/my_domain/category/onepage/';
    $path = $_SERVER['REQUEST_URI'];
    // Clean the uri
    //$path = str_replace('/', '', $page);
    $path = str_replace('.php', '', $path);
    //$path = str_replace('?s=', '', $path);
    $path = $path ? $path : 'default';

    $path_len = strlen($path);
    $last_char = substr($path, $path_len -1);
    //echo $last_char;
    $has_slash = strpos($last_char, "/");
    //echo $has_slash;
    if ($has_slash === 0) :
        $path = substr($path, 0, $path_len -1);
    elseif ($has_slash === null) :
        $path = substr($path, 0, $path_len);
    endif;
    //echo "path: ".$path; // '/www/public_html/index'
    $page = substr(strrchr($path, "/"), 1);
    echo "page: ".$page; // 'index'
}

$my_page_id = 31;
$my_page = 'mypage';

//echo "page: ".$page;
//echo "page_id ".$page_id;
if($page_id == $my_page_id || $page == $my_page) 
{
    // your stuff....
}

Enjoy!


You can also create a generic function to get the ID of the post, whether its outside or inside the loop (handles both the cases):

<?php

/**
 * @uses WP_Query
 * @uses get_queried_object()
 * @see get_the_ID()
 * @return int
 */
function get_the_post_id() {
  if (in_the_loop()) {
       $post_id = get_the_ID();
  } else {
       global $wp_query;
       $post_id = $wp_query->get_queried_object_id();
         }
  return $post_id;
} ?>

And simply do:

$page_id = get_the_post_id();