WP_Query::is_page( int|string|int[]|string[] $page = '' ): bool
- Since
- 3.1.0
- Source
wp-includes/class-wp-query.php:4578
Description
If the $page parameter is specified, this function will additionally check if the query is for one of the pages specified.
Compatibility
- WordPress
- since 3.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$pageint|string|int[]|string[]optional- Page ID, title, slug, path, or array of such to check against. Default empty.Default:
''
Return value
bool- Whether the query is for an existing single page.
Performance profile
How much work a call to WP_Query::is_page() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 4–43
- Plugin surface
- None
- Called by
- 3
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 51.
Nothing here hands control to plugin code.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_cache_get_last_changed()one call below WP_Query::is_page() - querycontent query
get_post()one call below WP_Query::is_page()
Further down the call graph this can also reach hook, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Query::is_page() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 4–6 | none |
!empty($page) | 10 | ->get_queried_object() |
!empty($page) | 21–32 | ->get_queried_object(), array_map() |
!empty($page) && !$page && $pagepath | 43 | ->get_queried_object(), array_map(), get_page_by_path() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 51 | 4–43 | 11 | |
| 8.5 | 51 | 4–43 | 11 | |
| 8.4 | 51 | 4–43 | 11 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 63 | 4–55 | 11 | |
| 8.2 | 63 | 4–55 | 11 | |
| 8.1 | 63 | 4–55 | 11 | |
| 7.4 | 63 | 4–55 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 2
- get_page_by_path()Retrieves a page given its path.
- WP_Query::get_queried_object()Retrieves the currently queried object.
Used by · 3
- WP_Query::generate_postdata()Generates post data.
- WP_Query::is_front_page()Determines whether the query is for the front page of the site.
- WP_Query::is_privacy_policy()Determines whether the query is for the Privacy Policy page.
Source code
public function is_page( $page = '' ) { if ( ! $this->is_page ) { return false; } if ( empty( $page ) ) { return true; } $page_obj = $this->get_queried_object(); if ( ! $page_obj ) { return false; } $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; }Changelog
Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.