Support » Developing with WordPress » How to check if is archive of a custom post type

  • If want to write a conditional logic test that looks like this

    if( is a tag, parent or child category, 
    custom taxonomy or basically any taxonomy 
    of custom post type FOOBAR ) {
               // do stuff
    }

    Any ideas?

    • This topic was modified 1 year, 4 months ago by TWD.
    • This topic was modified 1 year, 4 months ago by TWD.
    • This topic was modified 1 year, 4 months ago by TWD.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter TWD

    (@twd)

    I tried this but it didn’t work:

    if( is_post_type_archive( 'research-article' ) ) {
         // do stuff
    }

    Also, I am not sure if this works for ALL archives of that post type or just the parent CATEGORY archive.

    • This reply was modified 1 year, 4 months ago by TWD.
    Moderator bcworkz

    (@bcworkz)

    is_post_type_archive() relies upon the value of the “post_type” query var property of the main WP_Query object. This is not normally set for category queries (‘post’ post_type is assumed), so it returns false. It’s intended for actual post type archive queries like example.com/research-article/. Your code should do stuff with such a request. But not for a request like example.com/category/global-warming/.

    Thread Starter TWD

    (@twd)

    I’m a bit confused. Are you saying that I should do this:

    if( is_post_type_archive( $post_types = 'research-article' ) ) {
         // do stuff
    }
    Moderator bcworkz

    (@bcworkz)

    Yes, but only for actual post type archives (i.e. for a request like example.com/research-article/), not taxonomy archives. But do it like this:

    if( is_post_type_archive( 'research-article' ) ) {
         // do stuff
    }

    unless you really need to use $post_types variable in the “do stuff” code.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to check if is archive of a custom post type’ is closed to new replies.