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?
Thanks
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?
Thanks
You must be logged in to post a comment.
To get the current post type use
get_post_type()
. Then askget_post_type_object()
for all the data you need, for example the slug: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:
Update: @majick pointed out that this only works if you’ve set the rewrite slug for your CPT. Rewrite slug is optional when registering a CPT and defaults to post_type if not set.
The answers get confusing. And maybe Im as well but the headline question is:
If you mean post type archive landing-page, and when
is_post_type_archive()
returnstrue
, you want the slug that responing to current viewing archive:— END OF ANSWERING THE QUESTION —
Explanation:
You cant rely on the registered slug. WordPress is not either. For example, when calling
get_post_type_archive_link()
WordPress is checking the current rewrite rules for your install .Wherever you are, inside or outside loop, current archive or single post, reverse the
get_post_type_archive_link()
mechanism. (Permalinks enabled.)Considerations:
As mentioned here, the post type(s) in current query can be an
array
. You can go futher with your intensions with filter out the post type you look for, example:or
Another point of view:
Woocommerce example, is registered with ‘products’ post type object but in reality uses rewritten rule name (shop):
You can use this code and this code is working for me,
t should be noted that if
has_archive
is set to true while registering the Custom Post Type, the post type archive/cptslug/
will be internally rewritten to?post_type=cptslug
. So this would also meanis_post_type_archive()
will return true.Unfortunately, where the registered rewrite slug is different to the post type, you are not actually reliably getting the
post_type
. eg. if your post type wasmyplugin_cars
and your rewrite slug wascars
and you need to be gettingmyplugin_cars
then even this (to prevent errors if the current queried object is not a custom post type) will still fail:But because
is_post_type_archive
is true this is more reliable:But hang on, there’s more… turns out with a little testing it really isn’t that simple either… what if you are on a taxonomy archive page with multiple post types in the taxonomy..? Or assign post tags to a custom post type other than post? Or are on an author archive page? Date archive page? …or even have a complex
tax_query
ormeta_query
forWP_Query
?The only reliable answer (without testing for every possible archive case) is to loop the actual posts in the query… Here is the full function I came up with to work on both singular and archive pages, and allowing you to optionally pass a custom query object (or post object/post ID for singular posts):
This will reliably (did I say that?) return an array of post types if more than one is present, or a string with the single post type if there is only one type.
All you need to do is:
Example Usage (just for fun):
To see the effect, change the custom post type in the code to
post
, and add athumbtype-post
class attribute to your post thumbnail images…You can use this code:
use $posttype_slug var whatever you need