Can’t get post id on page that is a custom post type archive

EDIT

I get the idea that I might have misread your question

Just a few notes here

  • If archive-projects.php is a page template, rename it. You should not use archive as a prefix for a page template, or for that matter any other reserved template name. Page templates should be named page-{$name}.php or any other name with prefixes used by the template hierarchy. This is confusing to WordPress and messes with the template hierarchy

  • If this is a page template, you should get the ID with get_queried_object_id(); or display it with echo get_queried_object_id();

  • If this is a true archive page, you won’t get an ID

ORIGINAL ANSWER

Archive pages, whether post type archive pages, category archives, date archives etc, search pages, single pages and the homepage are pseudo pages, in other words, fake pages. They don’t have ID’s as they do not exist because they are not created pages which is saved in the db

These pages “inherits” the ID of the specific post or archive they display, although they still don’t have an ID themselves, accept the homepage, date archives, search pages and post type archive pages

So, in short, your custom post archive page will not have an ID.

PROOF OF CONCEPT

You can do a var_dump($wp_query) on your archive page, you will see that only real pages have ID’s

?><pre><?php var_dump($wp_query); ?></pre><?php 

Leave a Comment