WP_Query::is_page() WordPress Method

The WP_Query::is_page() method is used to check if the query is for a specific page. This is useful for checking if a particular page is being requested by the user.

WP_Query::is_page( int|string|int[]|string[] $page = '' ) #

Is the query for an existing single page?


Description

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

Top ↑

See also


Top ↑

Parameters

$page

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

Default value: ''


Top ↑

Return

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


Top ↑

Source

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

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

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

		$page_obj = $this->get_queried_object();

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

		if ( in_array( (string) $page_obj->ID, $page, true ) ) {
			return true;
		} elseif ( in_array( $page_obj->post_title, $page, true ) ) {
			return true;
		} elseif ( in_array( $page_obj->post_name, $page, true ) ) {
			return true;
		} else {
			foreach ( $page as $pagepath ) {
				if ( ! strpos( $pagepath, '/' ) ) {
					continue;
				}
				$pagepath_obj = get_page_by_path( $pagepath );

				if ( $pagepath_obj && ( $pagepath_obj->ID == $page_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