is_post_type_archive( string|array $post_types = '' )

Is the query for an existing post type archive page?


Description Description


Parameters Parameters

$post_types

(string|array) (Optional) Post type or array of posts types to check against.

Default value: ''


Top ↑

Return Return

(bool)


Top ↑

Source Source

File: wp-includes/query.php

function is_post_type_archive( $post_types = '' ) {
	global $wp_query;

	if ( ! isset( $wp_query ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
		return false;
	}

	return $wp_query->is_post_type_archive( $post_types );
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.1.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

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

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

    Notes
    This returns true for a page like /?post_type=my-custom-post-type, but not for /category/uncategorized/?post_type=custom. It’s only testing whether this is an archive of all posts of a given type. It is not checking for the existence of the post_type query parameter — that can be found by get_query_var('post_type').

    Also, depending on when this function runs it may or may not be run by nav_menu_item. For example:

    <?php
    function my_function( $query ){
        if ( is_post_type_archive( 'my_custom_post_type' ) ) {
             // Do stuff
        }
    }
    add_action( 'pre_get_posts', 'my_function' );
    ?>

    Whether “Do stuff” gets run in the menu depends on whether the theme use nav menus. A better usage would be:

    <?php
    function my_function( $query ) {
        if ( is_post_type_archive( 'my_custom_post_type' ) && ! empty( $query->query['post_type']  == 'my_custom_post_type' ) {
             // Do stuff
        }
    }
    add_action( 'pre_get_posts', 'my_function' );
    ?>

You must log in before being able to contribute a note or feedback.