get_pages( array|string $args = array() ): WP_Post[]|false
- Since
- 1.5.0, 6.3.0
- Source
wp-includes/post.php:6327
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 restrictionPage ID to return child and grandchild pages of. Note: The value of$hierarchicalhas no bearing on whether$child_ofreturns 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: trueWhether to return pages hierarchically. If false in conjunction with$child_ofalso being false, both arguments will be disregarded.$excludeint[]default: empty arrayArray of page IDs to exclude.$includeint[]default: empty arrayArray of page IDs to include. Cannot be used with$child_of,$parent,$exclude,$meta_key,$meta_value, or$hierarchical.$meta_keystringdefault: emptyOnly include pages with this meta key.$meta_valuestringdefault: emptyOnly include pages with this meta value. Requires$meta_key.$authorsstringdefault: emptyA comma-separated list of author IDs.$parentintdefault: -1, or no restrictionPage ID to return direct children of.$exclude_treestring|int[]default: empty arrayComma-separated string or array of page IDs to exclude.$numberintdefault: 0, or all pagesThe number of pages to return.$offsetintdefault: 0The 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
- Scaling
- Constant
- Instructions
- 8–11
- Plugin surface
- 2 hooks
- Called by
- 7
Reaches the database via get_pages().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 232.
Third-party callbacks on 'get_pages_query_args', 'get_pages' run inside this call, and their cost is not bounded by anything here.
7 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_pages()this function does it - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8–11 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 232 | 8–11 | 30 | |
| 8.5 | 232 | 8–11 | 30 | |
| 8.4 | 232 | 8–11 | 30 | 11 fewer instructions than PHP 8.3 |
| 8.3 | 243 | 10–16 | 30 | |
| 8.2 | 243 | 10–16 | 30 | |
| 8.1 | 243 | 10–16 | 30 | |
| 7.4 | 243 | 10–16 | 30 |
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:
- apply_filters( get_pages_query_args )filterline 6468 (+141 into the body)
Filters query arguments passed to WP_Query in get_pages.
Uses · 9
- wp_parse_args()Merges user defined arguments into defaults array.
- get_post_types()Gets a list of all registered post type objects.
- get_post_stati()Gets a list of post statuses.
- wp_parse_id_list()Cleans up an array, comma- or space-separated list of IDs.
- wp_parse_list()Converts a comma- or space-separated list of scalar values to an array.
- get_user_by()Retrieves user info by a given field.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_page_children()Identifies descendants of a given page ID in a list of page objects.
- WP_Query::__construct()Constructor.
Used by · 7
- WP_Customize_Manager::has_published_pages()Returns whether there are published pages.
- WP_Navigation_Block_Renderer::has_submenus()Returns whether or not a navigation has a submenu.
- WP_Posts_List_Table::_display_rows_hierarchical()
- get_body_class()Retrieves an array of the class names for the body element.
- render_block_core_page_list()Renders the `core/page-list` block on server.
- wp_dropdown_pages()Retrieves or displays a list of pages as a dropdown (select list).
- wp_list_pages()Retrieves or displays a list of pages (or hierarchical post type items) in list (li) format.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.