To find a page ID, open your WordPress dashboard and click on Pages > All Pages. Once the page has opened, you need to look at the URL in your web browser's address bar. Here, you will find the page ID number displayed within the page URL.
Bookmark this question. Show activity on this post. $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();
get_the_ID() Retrieves the ID of the current item in the WordPress Loop.
get_the_ID(); or $post->ID;
returns the current page or post id in Wordpress.
But you need to ensure that your post is saved in wordpress post table. Other wise you can't get the id , simply because of it is not an entry in wordpress database.
If it is a static page and it's not an entry in wordpress post then, get_the_ID()
didn't return anything.
For example : get_the_ID() didn't go to work in post archive pages , administration pages in wordpress backend etc.
So as per this question you are trying to get the id of the page that is a backend plugin setting page or any archive page .
UPDATE
Method to get the current post id in wordpress
(1) global $post; $post->ID();
(2) global $wp_query; $post_id = $wp_query->get_queried_object_id();
(3) global $wp_query; $post_id = $wp_query->post->ID;
(4) get_the_ID();
[ It is recommended that this tag must be within The Loop. ]
see this
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
ie get_the_ID() return the id of current $post .
(5) get_query_var('page_id')
[ it will not going to work if we use pretty permalink ]
https://codex.wordpress.org/Function_Reference/get_query_var
You can get ID
of the post in current page outside the loop using the technique below:
global $wp_query;
$post_id = $wp_query->post->ID;
$post = get_post( $post_id );
$slug = $post->post_name;
try to use below code to get the page id
get_the_ID();
I guess that this is the proper solution:
$id = get_queried_object_id();
which equals to:
function get_queried_object_id() {
global $wp_query;
return $wp_query->get_queried_object_id();
}
Chosen answer is working only if you put it in the Wordpress loop. Outside it will render useless.
This is working everywhere:
global $wp_query;
$postID = $wp_query->post->ID;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With