5

I have a custom post type archive archive-projects.php that I am using as a page template because I have some custom fields that I want to display. I am trying to get the post id of this page but either get null or the id of the first post in the archive depending on what I try. I have tried putting my code above the loop on my page but without any luck.

Apologies if this is a duplicate but I have tried searching many tutorials on this and still don't have an answer with my specific scenario of using a page template with a custom post type archive.

I have tried this code and it works but I am still stumped as to why none of the other methods seem to work. I'm reluctant to keep this as a solution because if the user changes the page slug this won't work anymore:

$my_page_id = get_id_by_slug('projects');

function get_id_by_slug($page_slug) {
$page = get_page_by_path($page_slug);
if ($page) {
    return $page->ID;
} else {
    return null;
}
}

I have tried the following without success (above my custom loop in my template):

get_queried_object_id(); // returns nothing
$page_id = $wp_query->get_queried_object_id(); // returns 0

global $post; // I've tried including this and removing with same effect
$post = $wp_query->post;
$post_id = $post->ID; // returns the id of the first post in my archive

Any ideas on what I am doing wrong or the right way to get the actual id of my projects page?

UPDATE My custom post type had has_archive set to true. I tried creating a custom template but due to WordPress hierarchy it was looking for an archive page. I solved my issue by setting has_archive to false and creating a custom template called page-template-projects.php. I was then able to setup a page called Projects and use my custom template, while being able to correctly get an ID for the page.

  • FWIW, this is exactly how I get custom fields on archive pages. It is brittle relying on a path/slug but I don't see another way of doing it either. – staypuftman Dec 11 '15 at 15:27
6

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 
  • Hi Pieter. A custom post type archive uses archive-postname.php as per the template hierarchy. It has to be called archive-projects to show the projects custom post type archive. If it's renamed, WordPress uses a standard layout for the Projects custom post type archive. – Greg Jan 19 '15 at 22:55
  • Yes, you are correct, so you should reread my ORIGINAL ANSWER. That page don't have an ID as it is not a page, so would any other page that is not a page. Place the following in your header inside a PHP tag: ?><pre><?php var_dump($wp_query); ?></pre><?php. You will see from this that there is no ID. You need to rethink your setup – Pieter Goosen Jan 20 '15 at 4:30
  • Thanks for your reply but I can't see another way to do this :) I need a custom post type archive and it needs to have custom fields. The 'page' does have an ID as I have a page called Projects that's using my template. I have a solution but wanted to know if there was a better way. If I create a page called 'Projects' using a custom template it just uses archive.php as per WordPress hierarchy. I haven't tried it yet but would setting 'has_archive' in my custom post type setup to false get around this? – Greg Jan 21 '15 at 4:46
  • Certainly, setting has_archive to false will make the post type to not have an archive. Just flush your rewrite rules afterwards. I'm not sure if this will solve your issue, but is worth trying. What is your idea with the custom fields. – Pieter Goosen Jan 21 '15 at 5:05
  • Woohoo! That did the trick. Can't believe I didn't think of it earlier. I have several custom post type archive pages that have an intro text area that needs to be custom as well as a featured image for each. I have been trying to solve this for ages. I appreciate your responses and help. – Greg Jan 21 '15 at 5:58
0

My custom post type had has_archive set to true. I tried creating a custom template but due to WordPress hierarchy it was looking for an archive page. I solved my issue by setting has_archive to false and creating a custom template called page-template-projects.php. (the name isn't important) I was then able to setup a page called Projects and use my custom template, while being able to correctly get an ID for the page.

If you are setting up a custom post type then take note of has_archive and make sure you use it accordingly. If you set this to false you can easily create your own template and custom query using WP_Query in your template to list out your posts belonging to your custom post type.

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.