Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

Thanks!

share|improve this question
add comment

1 Answer 1

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
add comment

Your Answer

 
discard

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.