0

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.

  • 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
0

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.

  • 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

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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