WP_Query::is_single( int|string|array $post = '' )

Is the query for an existing single post?


Description Description

Works for any post type excluding pages.

If the $post parameter is specified, this function will additionally check if the query is for one of the Posts specified.

See also See also


Top ↑

Parameters Parameters

$post

(int|string|array) (Optional) Post ID, title, slug, path, or array of such.

Default value: ''


Top ↑

Return Return

(bool) Whether the query is for an existing single post.


Top ↑

Source Source

File: wp-includes/class-wp-query.php

	public function is_single( $post = '' ) {
		if ( ! $this->is_single ) {
			return false;
		}

		if ( empty( $post ) ) {
			return true;
		}

		$post_obj = $this->get_queried_object();

		$post = array_map( 'strval', (array) $post );

		if ( in_array( (string) $post_obj->ID, $post ) ) {
			return true;
		} elseif ( in_array( $post_obj->post_title, $post ) ) {
			return true;
		} elseif ( in_array( $post_obj->post_name, $post ) ) {
			return true;
		} else {
			foreach ( $post as $postpath ) {
				if ( ! strpos( $postpath, '/' ) ) {
					continue;
				}
				$postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );

				if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
					return true;
				}
			}
		}
		return false;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
3.1.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

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