is_post_type_archive()WP 3.1.0

Checks if current user is on the archive page of custom post type.

The custom post type archive page is the page that displays the new post type (/?post_type=custom). If, for example, the new post type is called 'book', the page will have the URL: http://example.com/book/.

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').

The function also works in admin on the page with the posts table.

Used By: is_shop()
1 time — 0.00003 sec (very fast) | 50000 times — 0.03 sec (speed of light)

No Hooks.

Return

true|false. If this is an archive page with posts of any type, the function will returns true, otherwise false.

Usage

if( is_post_type_archive( $post_types ) ){
	// ...
}
$post_types(string|string[])
Post type or array of posts types to check against.
Default: ''

Examples

0

#1 Archive page of a new post type

This example shows how to determine if the current page is the archive page of an custom post type. Let's display the title of the new post type.

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

#2 Change base WP query on any post_type archive page

This function check all post type posts archive page only. But what if we need to handle any query of custom post type archive page, for example, detect archives page of custom post type category posts. To do this you can use the following check:

add_action( 'pre_get_posts', 'wpdocs_my_function' );

function wpdocs_my_function( $query ) {

	if (
		is_post_type_archive( 'my_custom_post_type' )
		&&
		'my_custom_post_type' === $query->query['post_type']
	) {
		 // Do stuff
	}
}

Notes

  • Global. WP_Query. $wp_query WordPress Query object.

Changelog

Since 3.1.0 Introduced.

is_post_type_archive() code WP 6.6.2

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 );
}