WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I get the ID of a post (or page) based on the $query_vars variable? I want to do something like


But I don't have a reference page that has the list of $query_vars perams. The code will be using the template_redirect action hook.

share|improve this question
up vote 3 down vote accepted

When viewing a single post, get_the_ID(); used within the loop will return the current post's ID.

But I don't have a reference page that has the list of $query_vars params...

Dumping it

global $wp_query;

would provide such a reference.

Where you'd subsequently see that $wp_query->query_vars['page_id'] yields the page ID.

share|improve this answer

If you're on a single post or page view, try using get_queried_object_id().

Or, if you don't know what type of view you're on, use get_queried_object() and then analyse the object returned to find out whether its a post, a taxonomy term, or something different.

share|improve this answer
Thanks, I will note this. – 21zna9 Nov 16 '11 at 22:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.