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.

I am doing some testing with custom post types.

Assume I have various page templates and a custom post type template that share one footer.php file. In the footer, I call do_dis_function() which is simply defined as:

function do_dis_function() {
        global $post;

        the_ID();
        // echo get_the_ID();
        // echo $post->ID;
}

In both cases, in the page and/or CPT template, an ID displays... but only in the page template is it consistent.

Upon further testing, I realize that if I am viewing the custom post type template, the ID that's echoed, which seems random at first, looks to be an ID from a custom post type entry which shares the same category.

So to break it down, I have 5 projects, 2 categories. 3 of the projects share the same category. Their IDs are 16, 22 and 41. If I'm viewing post 22, the ID echoed is either 16 or 41.

Dafuq. Someone please explain.

share|improve this question
    
Do you have any secondary queries, for instance for other projects in the same category? –  Stephen Harris Nov 15 '12 at 18:17
    
saldkadlksqwldsda! yeah... get_related_portfolio_projects() Didn't see it, right before the footer call. I assume I have to then just reset the query? –  mousesports Nov 15 '12 at 18:21
    
Yeah that worked. Thanks @StephenHarris –  mousesports Nov 15 '12 at 18:27

1 Answer 1

the_ID()

must be within The Loop.

from the_ID codex page

global $post will merely get the last post that was set as the global post object which will depend greatly on what is done in the rest of the request. If somewhere in your header template, page template or content template you change or modify the global $post object that's the one you will get back when you bring it into your function.

share|improve this answer
    
Beauty, thanks for the explanation but that wasn't my issue. I had manipulated the query and screwed anything that came after. –  mousesports Nov 15 '12 at 18:32

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.