WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I discover the custom post type slug when I'm on an archive page?

For instance if /products/ fires the archive-products.php template, how (pragmatically) do I get the post type slug?


share|improve this question

To get the current post type use get_post_type(). Then ask get_post_type_object() for all the data you need, for example the slug:

$post_type = get_post_type();
if ( $post_type )
    $post_type_data = get_post_type_object( $post_type );
    $post_type_slug = $post_type_data->rewrite['slug'];
    echo $post_type_slug;
share|improve this answer
I think (hadn't tested) get_queried_object() would get to same info in less moves. – Rarst Oct 9 '12 at 1:15
@Rarst Maybe, but I think the code I suggested is easier to understand. – toscho Oct 9 '12 at 1:18
Toscho's solutions is wrong, because get_post_type returns the post type of current page, and, when you are into archive page, this function alwasy return "page". I'm sarching to solve the same: When I am into archive page of 'books' (4 example), I want this: 'books'. When I get it I'll post it. – eMarine Jun 25 '13 at 9:58

I'm using this outside of the loop on the archive.php template to get which custom post archive I'm on.

It's a combo of the methods that both @toscho and @Rarst recommended:

$post_type = get_queried_object();
echo $post_type->rewrite['slug'];
share|improve this answer
when I tried this I got Notice: Undefined property: stdClass::$rewrite in ***\wp-content\themes\marks-remarks\archive.php on line 4 – patrickzdb Oct 20 '15 at 13:37
if( get_post_type( get_the_ID() ) == 'projects' )
  //enter code for this post type
share|improve this answer

Your Answer


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.