WordPress Get the Page ID outside the loop

PhpWordpress

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

If you're using pretty permalinks, get_query_var('page_id') won't work.

Instead, get the queried object ID from the global $wp_query:

// Since 3.1 - recommended!
$page_object = get_queried_object();
$page_id     = get_queried_object_id();


// "Dirty" pre 3.1
global $wp_query;

$page_object = $wp_query->get_queried_object();
$page_id     = $wp_query->get_queried_object_id();

Solution 2 - Php

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();

Solution 3 - Php

Use this global $post instead:

global $post;
echo $post->ID;

Solution 4 - Php

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)

Solution 5 - Php

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.

Solution 6 - Php

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];

Solution 7 - Php

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

Solution 8 - Php

This function get id off a page current.

get_the_ID();

Solution 9 - Php

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

global $post;
echo $post->ID;

Solution 10 - Php

This is the correct code.

echo $post->ID;

Solution 11 - Php

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAtifView Question on Stackoverflow
Solution 1 - PhpTheDeadMedicView Answer on Stackoverflow
Solution 2 - PhpNadeem KhanView Answer on Stackoverflow
Solution 3 - PhpryscriptView Answer on Stackoverflow
Solution 4 - PhpbanestoView Answer on Stackoverflow
Solution 5 - PhpJBBView Answer on Stackoverflow
Solution 6 - PhpEddView Answer on Stackoverflow
Solution 7 - Phpnikc.orgView Answer on Stackoverflow
Solution 8 - PhpjruzafaView Answer on Stackoverflow
Solution 9 - PhpBraj Kishor SahView Answer on Stackoverflow
Solution 10 - PhpBannaView Answer on Stackoverflow
Solution 11 - PhpedcvView Answer on Stackoverflow