Using WordPress ‘is_post_type_archive()’ PHP function

The is_post_type_archive() WordPress PHP function determines whether the query is for an existing post type archive page.

Usage

is_post_type_archive('custom_post_type');

Parameters

  • $post_types (string|string[]) – Optional. Post type or array of post types to check against. Default: ”.

More information

See WordPress Developer Resources: is_post_type_archive()

Examples

Display custom post type title

If the current page is an archive of a custom post type, display the custom post type title:

if (is_post_type_archive()) {
    ?><h1><?php post_type_archive_title(); ?></h1><?php
}

Conditionally enqueue styles/scripts with custom post type archive page

Add styles and scripts to the custom post type archive page:

add_action('wp_enqueue_scripts', function() {
    if (is_post_type_archive('snippets')) {
        wp_enqueue_style('wpdocs-style-name', get_stylesheet_uri());
        wp_enqueue_script('wpdocs-script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true);
    }
});

Modify query for custom post type archive

Modify the query for a custom post type archive:

function wpdocs_my_function($query) {
    if (is_post_type_archive('my_custom_post_type')) {
        // Do stuff
    }
}
add_action('pre_get_posts', 'wpdocs_my_function');

Check if custom post type archive page

Check if the current page is a custom post type archive page:

if (is_post_type_archive('my_custom_post_type')) {
    // Do stuff
}

Check if the query is for any of the given post types

Check if the query is for an archive page of any of the given post types:

$post_types = array('custom_post_type1', 'custom_post_type2');
if (is_post_type_archive($post_types)) {
    // Do stuff
}