wppaste
WordPress

wp_list_pages( array|string $args = '' ): void|string

Since
1.5.0, 4.7.0
Source
wp-includes/post-template.php:1303
Retrieves or displays a list of pages (or hierarchical post type items) in list (li) format.

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' option

    PHP date format to use for the listed pages. Relies on the 'show_date' parameter.
  • $depthintdefault: 0

    Number 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: true

    Whether or not to echo the list of pages.
  • $excludestringdefault: empty

    Comma-separated list of page IDs to exclude.
  • $includearraydefault: empty

    Comma-separated list of page IDs to include.
  • $link_afterstringdefault: null

    Text or HTML to follow the page link label.
  • $link_beforestringdefault: null

    Text 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: empty

    Whether 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 used

    Walker 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

Reaches the database via get_pages().

Scaling
Constant

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

Instructions
65–115

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

Plugin surface
2 hooks

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

Called by
2

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

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_pages()called directly
  • cacheobject cachewp_cache_get()one call below wp_list_pages()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
!$parsed_args && empty($pages)65–69get_option(), __(), wp_parse_args(), apply_filters(), get_pages(), apply_filters()
$parsed_args && empty($pages)71–75get_option(), __(), wp_parse_args(), explode(), apply_filters(), get_pages(), apply_filters()
!$parsed_args && !empty($pages) && is_page()86–95get_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–100get_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–99get_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–101get_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–106get_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–105get_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–109get_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–115get_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

PHPCompiledExecutedBranchesNotes
8.6-dev12065–114111 fewer instruction than PHP 8.5
8.512165–11511
8.412165–115116 fewer instructions than PHP 8.3
8.312771–12111
8.212771–12111
8.112771–12111
7.412771–12111

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:

  1. apply_filters( wp_list_pages_excludes )filterline 1343 (+40 into the body)

    Filters the array of pages to exclude from the pages list.

  2. apply_filters( wp_list_pages )filterline 1384 (+81 into the body)

    Filters the HTML output of the pages to list.

Uses · 12

Used by · 2

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.

  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.

4.7.0
Added the item_spacing argument.from the docblock
1.5.0
Introduced.from the docblock

About 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.