1

Is there any way to determine if a particular custom post type will be displayed on default archive pages or search results?

e.g. Given a post type named (badly) books, is there a way to tell via code any of the following:

  • If books show up on date archives
  • If books show up in search results
  • If books show up in author archives

I know how to make custom post types show up in these, but I've been doing a bit of digging and can see no clean way to determine if a particular custom post type will show up in any of the above or not.

The main reason I'm looking for this is that I'm creating custom breadcrumb bars for a theme... and it would be nice to be able to show an authors breadcrumb bar if the custom post being viewed supports author functionality and shows up in the author archive.

3

You can get the post type object and check its properties. For example:

$post_type = get_post_type_object( 'books' );
if( $post_type->exclude_from_search ) {
    // The post type is not included in search results
}
if( $post_type->has_archive ) {
    // The post type has archive
}

As far I know, custom post types are not included in dates archives nor author archives if you don't code for it (using pre_get_posts for example). I mean, those options are not available while registering a post type. So, there is no way to check it from post type object.

  • Thanks! I searched all over hoping to find something, but finally decided to ask as I was turning up nothing in the source. – Privateer May 29 '16 at 18:10
0

If you are on the front, on template you can use the is_post_type_archive( $post_types ) conditional tag function. $post_types can be a string or an array of posts types to check against.

  • I'm not looking to see whether I am on a post type archive or not, but whether to see if the currently viewed post type is displayed on one of the builtin archive pages. Thanks though. – Privateer May 29 '16 at 15:30

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.