wp_list_pages( array|string $args = '' ): void|string
- Since
- 1.5.0, 4.7.0
- Source
wp-includes/post-template.php:1303
Compatibility
- WordPress
- since 4.7.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 generate a list of pages. See get_pages() for additional arguments.Default:
''$child_ofintdefault: 0 (all pages)Display only the sub-pages of a single page by ID.$authorsstringdefault: empty (all authors)Comma-separated list of author IDs.$date_formatstringdefault: is the value of 'date_format' optionPHP date format to use for the listed pages. Relies on the 'show_date' parameter.$depthintdefault: 0Number of levels in the hierarchy of pages to include in the generated list. Accepts -1 (any depth), 0 (all pages), 1 (top-level pages only), and n (pages to the given n depth).$echobooldefault: trueWhether or not to echo the list of pages.$excludestringdefault: emptyComma-separated list of page IDs to exclude.$includearraydefault: emptyComma-separated list of page IDs to include.$link_afterstringdefault: nullText or HTML to follow the page link label.$link_beforestringdefault: nullText or HTML to precede the page link label.$post_typestringdefault: 'page'Post type to query for.$post_statusstring|arraydefault: 'publish'Comma-separated list or array of post statuses to include.$show_datestringdefault: emptyWhether to display the page publish or modified date for each page. Accepts 'modified' or any other value. An empty value hides the date.$sort_columnstringdefault: 'post_title'Comma-separated list of column names to sort the pages by. Accepts 'post_author', 'post_date', 'post_title', 'post_name', 'post_modified', 'post_modified_gmt', 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'.$title_listringdefault: 'Pages'List heading. Passing a null or empty value will result in no heading, and the list will not be wrapped with unordered list<ul>tags.$item_spacingstringdefault: 'preserve'Whether to preserve whitespace within the menu's HTML. Accepts 'preserve' or 'discard'.$walkerWalkerdefault: empty which results in a Walker_Page instance being usedWalker instance to use for listing pages.
Return value
void|string- Void if 'echo' argument is true, HTML list of pages if 'echo' is false.
Performance profile
How much work a call to wp_list_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
- 65–115
- Plugin surface
- 2 hooks
- Called by
- 2
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 121.
Third-party callbacks on 'wp_list_pages_excludes', 'wp_list_pages' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - querycontent query
get_pages()called directly - cacheobject cache
wp_cache_get()one call below wp_list_pages() - serializeserialisation
maybe_unserialize()one call below wp_list_pages()
Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_list_pages() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$parsed_args && empty($pages) | 65–69 | get_option(), __(), wp_parse_args(), apply_filters(), get_pages(), apply_filters() |
$parsed_args && empty($pages) | 71–75 | get_option(), __(), wp_parse_args(), explode(), apply_filters(), get_pages(), apply_filters() |
!$parsed_args && !empty($pages) && is_page() | 86–95 | get_option(), __(), wp_parse_args(), apply_filters(), get_pages(), is_page(), get_queried_object_id(), walk_page_tree(), apply_filters() |
!$parsed_args && !empty($pages) && !is_page() | 89–100 | get_option(), __(), wp_parse_args(), apply_filters(), get_pages(), is_page(), is_attachment(), get_queried_object_id(), walk_page_tree(), apply_filters() |
!$parsed_args && !empty($pages) && !is_page() && !is_attachment() && !$wp_query && !is_singular() | 90–99 | get_option(), __(), wp_parse_args(), apply_filters(), get_pages(), is_page(), is_attachment(), is_singular(), walk_page_tree(), apply_filters() |
$parsed_args && !empty($pages) && is_page() | 92–101 | get_option(), __(), wp_parse_args(), explode(), apply_filters(), get_pages(), is_page(), get_queried_object_id(), walk_page_tree(), apply_filters() |
$parsed_args && !empty($pages) && !is_page() | 95–106 | get_option(), __(), wp_parse_args(), explode(), apply_filters(), get_pages(), is_page(), is_attachment(), get_queried_object_id(), walk_page_tree(), apply_filters() |
$parsed_args && !empty($pages) && !is_page() && !is_attachment() && !$wp_query && !is_singular() | 96–105 | get_option(), __(), wp_parse_args(), explode(), apply_filters(), get_pages(), is_page(), is_attachment(), is_singular(), walk_page_tree(), apply_filters() |
!$parsed_args && !empty($pages) && !is_page() && !is_attachment() && !$wp_query && is_singular() | 99–109 | get_option(), __(), wp_parse_args(), apply_filters(), get_pages(), is_page(), is_attachment(), is_singular(), get_queried_object(), is_post_type_hierarchical(), walk_page_tree(), apply_filters() |
$parsed_args && !empty($pages) && !is_page() && !is_attachment() && !$wp_query && is_singular() | 105–115 | get_option(), __(), wp_parse_args(), explode(), apply_filters(), get_pages(), is_page(), is_attachment(), is_singular(), get_queried_object(), is_post_type_hierarchical(), walk_page_tree(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 120 | 65–114 | 11 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 121 | 65–115 | 11 | |
| 8.4 | 121 | 65–115 | 11 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 127 | 71–121 | 11 | |
| 8.2 | 127 | 71–121 | 11 | |
| 8.1 | 127 | 71–121 | 11 | |
| 7.4 | 127 | 71–121 | 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.
Hooks and filters fired · 2
2 hooks fire while wp_list_pages() runs, in this order:
- apply_filters( wp_list_pages_excludes )filterline 1343 (+40 into the body)
Filters the array of pages to exclude from the pages list.
- apply_filters( wp_list_pages )filterline 1384 (+81 into the body)
Filters the HTML output of the pages to list.
Uses · 12
- get_option()Retrieves an option value based on an option name.
- __()Retrieves the translation of $text.
- wp_parse_args()Merges user defined arguments into defaults array.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- get_pages()Retrieves an array of pages (or hierarchical post type items).
- is_page()Determines whether the query is for an existing single page.
- is_attachment()Determines whether the query is for an existing attachment page.
- get_queried_object_id()Retrieves the ID of the currently queried object.
- is_singular()Determines whether the query is for an existing single post of any post type (post, attachment, page, custom post types).
- get_queried_object()Retrieves the currently queried object.
- is_post_type_hierarchical()Determines whether the post type is hierarchical.
- walk_page_tree()Retrieves HTML list content for page list.
Used by · 2
- WP_Widget_Pages::widget()Outputs the content for the current Pages widget instance.
- wp_page_menu()Displays or retrieves a list of pages with an optional home link.
Source code
function wp_list_pages( $args = '' ) { $defaults = array( 'depth' => 0, 'show_date' => '', 'date_format' => get_option( 'date_format' ), 'child_of' => 0, 'exclude' => '', 'title_li' => __( 'Pages' ), 'echo' => 1, 'authors' => '', 'sort_column' => 'menu_order, post_title', 'link_before' => '', 'link_after' => '', 'item_spacing' => 'preserve', 'walker' => '', ); $parsed_args = wp_parse_args( $args, $defaults ); if ( ! in_array( $parsed_args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { // Invalid value, fall back to default. $parsed_args['item_spacing'] = $defaults['item_spacing']; } $output = ''; $current_page = 0; // Sanitize, mostly to keep spaces out. $parsed_args['exclude'] = preg_replace( '/[^0-9,]/', '', $parsed_args['exclude'] ); // Allow plugins to filter an array of excluded pages (but don't put a nullstring into the array). $exclude_array = ( $parsed_args['exclude'] ) ? explode( ',', $parsed_args['exclude'] ) : array(); /** * Filters the array of pages to exclude from the pages list. * * @since 2.1.0 * * @param string[] $exclude_array An array of page IDs to exclude. */ $parsed_args['exclude'] = implode( ',', apply_filters( 'wp_list_pages_excludes', $exclude_array ) ); $parsed_args['hierarchical'] = 0; // Query pages. $pages = get_pages( $parsed_args ); if ( ! empty( $pages ) ) { if ( $parsed_args['title_li'] ) { $output .= '<li class="pagenav">' . $parsed_args['title_li'] . '<ul>'; } global $wp_query; if ( is_page() || is_attachment() || $wp_query->is_posts_page ) { $current_page = get_queried_object_id(); } elseif ( is_singular() ) { $queried_object = get_queried_object(); if ( is_post_type_hierarchical( $queried_object->post_type ) ) { $current_page = $queried_object->ID; } } $output .= walk_page_tree( $pages, $parsed_args['depth'], $current_page, $parsed_args ); if ( $parsed_args['title_li'] ) { $output .= '</ul></li>'; } } /** * Filters the HTML output of the pages to list. * * @since 1.5.1 * @since 4.4.0 `$pages` added as arguments. * * @see wp_list_pages() * * @param string $output HTML output of the pages list. * @param array $parsed_args An array of page-listing arguments. See wp_list_pages() * for information on accepted arguments. * @param WP_Post[] $pages Array of the page objects.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.
item_spacing argument.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/post-template.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.