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

Thought this would be straight forward... given a tag page such as http://domain.com/blog/tag/tag-1/ how can I retrieve the post_id while on that page?

The usual suspects such as url_to_postid and $post->ID or $wp_query->post->ID do not return anything. The hook that calls the function is running during init.


share|improve this question
up vote 0 down vote accepted

A tag page would be like other archive pages in that a loop of posts would be displayed, even if only one post has been assigned that tag.

You're check for post ID needs to happen during the <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> loop.

share|improve this answer
Ah... don't think I can do that since this is a plugin and not a theme. Is there any way I can treat a tag page differently from within my plugin? – Jonathan van Clute Oct 7 '11 at 4:23
You may want to look into using add_action( 'the_post', 'your_action_function' ); in your plugin. That hooks in and returns an array of post data to the specified function. – Jeremy Felt Oct 7 '11 at 4:41
AWESOME! That looks like it's precisely what I needed. Thanks!! – Jonathan van Clute Oct 7 '11 at 5:13
I've run into one unfortunate problem with this solution. The hook I need to use this with, must execute at one of several possible points, including init, wp_loaded, and a few others. Unfortunately all of these cause the passing of the post data to fail, and so I'm not able to pick up the fact that I'm on a tag page. Any idea how I can use is_tag or similar functions within init? – Jonathan van Clute Oct 7 '11 at 20:42

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.