I have woocommerce page for my products that is treated like an archive page. I need to get the ID for the archive page but obviously if I use $post->ID or get_the_id(), etc. I get the id for the most recent post on that page.


If this is a true page, then you can get the ID of that specific page with get_queried_object_id(). This works and will return the ID on the specific pages, single, category, taxonomy, tag and author pages where it is used. True date and time archives and the homepage will not have ID's

You can do the following

$page_id = get_queried_object_id();
echo $page_id;

Just one very important note, query_posts breaks the main query which breaks the functionality above. This will lead to incorrect data. So make sure that you never ever use query_posts

  • When I use that it get an ID of 0 when it should be 6. I'm not entirely sure how woocommerce loops through the products. However, I know better than to use query_posts – dcp3450 Jul 20 '15 at 6:41
  • Then your page is not a page but an archive template in which case you will get 0. If you need to use a page as an archive page, you need to set has_archive to false when registering the custom post type otherwise your custom page template will never be used and my code will always return 0 as expected. Just remember to flush rewrites after the change – Pieter Goosen Jul 20 '15 at 6:46
  • 1
    I had to use a get_option provided by woocommerce – dcp3450 Jul 20 '15 at 6:49
  • Meaning what exactly? – Pieter Goosen Jul 20 '15 at 6:54
  • 1
    Glad you solved the issue. Well, you can post that as an answer and accept it as the correct. ;-) – Pieter Goosen Jul 20 '15 at 18:55

Do you really need to know the ID, for that see @PieterGoosens's answer, or do you just need to know if you are at the post type product archive? The latter can easily be achieved by using is_post_type_archive():

if ( is_post_type_archive( 'product' ) ) {
     // code
  • I use the page id to grab an setting saved via a custom metabox. I found that woocommerce stores that value as an option. – dcp3450 Jul 20 '15 at 18:51

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