wppaste
WordPress

get_pages( array|string $args = array() ): WP_Post[]|false

Since
1.5.0, 6.3.0
Source
wp-includes/post.php:6274
Retrieves an array of pages (or hierarchical post type items).

Compatibility

WordPress
since 6.3.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

$argsarray|stringoptional
Array or string of arguments to retrieve pages.Default: array()
  • $child_ofintdefault: 0, or no restriction

    Page ID to return child and grandchild pages of. Note: The value of $hierarchical has no bearing on whether $child_of returns hierarchical results.
  • $sort_orderstringdefault: 'ASC'

    How to sort retrieved pages. Accepts 'ASC', 'DESC'.
  • $sort_columnstringdefault: 'post_title'

    What columns to sort pages by, comma-separated. Accepts 'post_author', 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'commentcount'. 'post' can be omitted for any values that start with it.
  • $hierarchicalbooldefault: true

    Whether to return pages hierarchically. If false in conjunction with $child_of also being false, both arguments will be disregarded.
  • $excludeint[]default: empty array

    Array of page IDs to exclude.
  • $includeint[]default: empty array

    Array of page IDs to include. Cannot be used with $child_of, $parent, $exclude, $meta_key, $meta_value, or $hierarchical.
  • $meta_keystringdefault: empty

    Only include pages with this meta key.
  • $meta_valuestringdefault: empty

    Only include pages with this meta value. Requires $meta_key.
  • $authorsstringdefault: empty

    A comma-separated list of author IDs.
  • $parentintdefault: -1, or no restriction

    Page ID to return direct children of.
  • $exclude_treestring|int[]default: empty array

    Comma-separated string or array of page IDs to exclude.
  • $numberintdefault: 0, or all pages

    The number of pages to return.
  • $offsetintdefault: 0

    The number of pages to skip before returning. Requires $number.
  • $post_typestringdefault: 'page'

    The post type to query.
  • $post_statusstring|arraydefault: 'publish'

    A comma-separated list or array of post statuses to include.

Return value

WP_Post[]|false
Array of pages (or hierarchical post type items). Boolean false if the specified post type is not hierarchical or the specified status is not supported by the post type.

Performance profile

How much work a call to get_pages() 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

Reaches the database via get_pages().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8–11

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 232.

Plugin surface
2 hooks

Third-party callbacks on 'get_pages_query_args', 'get_pages' run inside this call, and their cost is not bounded by anything here.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_pages()this function does it
  • hookthird-party callbacksapply_filters()called directly

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_pages() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8–11none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2328–1130
8.52328–1130
8.42328–113011 fewer instructions than PHP 8.3
8.324310–1630
8.224310–1630
8.124310–1630
7.424310–1630

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.

Hooks and filters fired · 2

2 hooks fire while get_pages() runs, in this order:

  1. apply_filters( get_pages_query_args )filterline 6415 (+141 into the body)

    Filters query arguments passed to WP_Query in get_pages.

  2. apply_filters( get_pages )filterline 6449 (+175 into the body)

    Filters the retrieved list of pages.

Uses · 9

Used by · 7

Source code

function get_pages( $args = array() ) {	$defaults = array(		'child_of'     => 0,		'sort_order'   => 'ASC',		'sort_column'  => 'post_title',		'hierarchical' => 1,		'exclude'      => array(),		'include'      => array(),		'meta_key'     => '',		'meta_value'   => '',		'authors'      => '',		'parent'       => -1,		'exclude_tree' => array(),		'number'       => '',		'offset'       => 0,		'post_type'    => 'page',		'post_status'  => 'publish',	); 	$parsed_args = wp_parse_args( $args, $defaults ); 	$number       = (int) $parsed_args['number'];	$offset       = (int) $parsed_args['offset'];	$child_of     = (int) $parsed_args['child_of'];	$hierarchical = $parsed_args['hierarchical'];	$exclude      = $parsed_args['exclude'];	$meta_key     = $parsed_args['meta_key'];	$meta_value   = $parsed_args['meta_value'];	$parent       = $parsed_args['parent'];	$post_status  = $parsed_args['post_status']; 	// Make sure the post type is hierarchical.	$hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );	if ( ! in_array( $parsed_args['post_type'], $hierarchical_post_types, true ) ) {		return false;	} 	if ( $parent > 0 && ! $child_of ) {		$hierarchical = false;	} 	// Make sure we have a valid post status.	if ( ! is_array( $post_status ) ) {		$post_status = explode( ',', $post_status );	}	if ( array_diff( $post_status, get_post_stati() ) ) {		return false;	} 	$query_args = array(		'orderby'                => 'post_title',		'order'                  => 'ASC',		'post__not_in'           => wp_parse_id_list( $exclude ),		'meta_key'               => $meta_key,		'meta_value'             => $meta_value,		'posts_per_page'         => -1,		'offset'                 => $offset,		'post_type'              => $parsed_args['post_type'],		'post_status'            => $post_status,		'update_post_term_cache' => false,		'update_post_meta_cache' => false,		'ignore_sticky_posts'    => true,		'no_found_rows'          => true,	); 	if ( ! empty( $parsed_args['include'] ) ) {		$child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include.		$parent   = -1;		unset( $query_args['post__not_in'], $query_args['meta_key'], $query_args['meta_value'] );		$hierarchical           = false;		$query_args['post__in'] = wp_parse_id_list( $parsed_args['include'] );	} 	if ( ! empty( $parsed_args['authors'] ) ) {		$post_authors = wp_parse_list( $parsed_args['authors'] ); 		if ( ! empty( $post_authors ) ) {			$query_args['author__in'] = array();			foreach ( $post_authors as $post_author ) {				// Do we have an author id or an author login?

Changelog

Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.3.0
Use WP_Query internally.from the docblock
1.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/post.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.