WP_Query::is_single() WordPress Method

The WP_Query::is_single() method is used to check if the current query is for a single post.

WP_Query::is_single( int|string|int[]|string[] $post = '' ) #

Is the query for an existing single post?


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.

Top ↑

See also


Top ↑

Parameters

$post

(int|string|int[]|string[])(Optional) Post ID, title, slug, path, or array of such to check against.

Default value: ''


Top ↑

Return

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


Top ↑

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, true ) ) {
			return true;
		} elseif ( in_array( $post_obj->post_title, $post, true ) ) {
			return true;
		} elseif ( in_array( $post_obj->post_name, $post, true ) ) {
			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
VersionDescription
3.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More