Walker::paged_walk( array $elements, int $max_depth, int $page_num, int $per_page, mixed $args ): string
- Since
- 2.7.0, 5.3.0
- Source
wp-includes/class-wp-walker.php:292
Description
Given an array of hierarchical elements, the maximum depth, a specific page number, and number of elements per page, this function first determines all top level root elements belonging to that page, then lists them and all of their children in hierarchical order.
$max_depth = 0 means display all levels.
$max_depth > 0 specifies the number of display levels.
Compatibility
- WordPress
- since 5.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
$elementsarray- An array of elements.
$max_depthint- The maximum hierarchical depth.
$page_numint- The specific page number, beginning with 1.
$per_pageint- Number of elements per page.
$argsmixed- Optional additional arguments.
Return value
string- XHTML of the specified page of elements.
Performance profile
How much work a call to Walker::paged_walk() 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
- Light
- Scaling
- Scales with input
- Instructions
- 11–95
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
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 174.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost Walker::paged_walk() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11–70 | none |
!empty($elements) && !$max_depth && !empty($value) | 42–77 | array_reverse() |
!empty($elements) && !$max_depth && $max_depth !== -1 && empty($value) | 45–77 | ceil() |
!empty($elements) && !$max_depth && $max_depth !== -1 && !empty($value) | 52–84 | ceil(), array_reverse() |
!empty($elements) && !$max_depth && $max_depth !== -1 && empty($value) && !$top_level_elements && $total_top && $end | 60–74 | ->unset_children() |
!empty($elements) && !$max_depth && !$page_num && !$per_page && empty($value) | 63–84 | ceil(), ceil() |
!empty($elements) && !$max_depth && $max_depth !== -1 && !empty($value) && !$top_level_elements && $total_top && $end | 67–81 | array_reverse(), ->unset_children() |
!empty($elements) && !$max_depth && $max_depth !== -1 && empty($value) && !$top_level_elements && $total_top && $end | 67–81 | ceil(), ->unset_children() |
!empty($elements) && !$max_depth && !$page_num && !$per_page && !empty($value) | 70–91 | ceil(), ceil(), array_reverse() |
!empty($elements) && !$max_depth && $max_depth !== -1 && !empty($value) && !$top_level_elements && $total_top && $end | 74–88 | ceil(), array_reverse(), ->unset_children() |
!empty($elements) && !$max_depth && !$page_num && !$per_page && empty($value) && !$top_level_elements && $total_top && $end | 77–88 | ceil(), ceil(), ->unset_children() |
!empty($elements) && !$max_depth && !$page_num && !$per_page && !empty($value) && !$top_level_elements && $total_top && $end | 84–95 | ceil(), ceil(), array_reverse(), ->unset_children() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 174 instructions, 11–95 executed per call, 33 branches. The work does not change between versions.
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
- Walker::display_element()Traverses elements to create list from elements.
- Walker::unset_children()Unsets all the children for a given top level element.
Source code
public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) { $output = ''; $max_depth = (int) $max_depth; if ( empty( $elements ) || $max_depth < -1 ) { return $output; } $parent_field = $this->db_fields['parent']; $count = -1; if ( -1 === $max_depth ) { $total_top = count( $elements ); } if ( $page_num < 1 || $per_page < 0 ) { // No paging. $paging = false; $start = 0; if ( -1 === $max_depth ) { $end = $total_top; } $this->max_pages = 1; } else { $paging = true; $start = ( (int) $page_num - 1 ) * (int) $per_page; $end = $start + $per_page; if ( -1 === $max_depth ) { $this->max_pages = (int) ceil( $total_top / $per_page ); } } // Flat display. if ( -1 === $max_depth ) { if ( ! empty( $args[0]['reverse_top_level'] ) ) { $elements = array_reverse( $elements ); $oldstart = $start; $start = $total_top - $end; $end = $total_top - $oldstart; } $empty_array = array(); foreach ( $elements as $e ) { ++$count; if ( $count < $start ) { continue; } if ( $count >= $end ) { break; } $this->display_element( $e, $empty_array, 1, 0, $args, $output ); } return $output; } /* * Separate elements into two buckets: top level and children elements. * Children_elements is two dimensional array, e.g. * $children_elements[10][] contains all sub-elements whose parent is 10. */ $top_level_elements = array(); $children_elements = array(); foreach ( $elements as $e ) { if ( empty( $e->$parent_field ) ) { $top_level_elements[] = $e; } else { $children_elements[ $e->$parent_field ][] = $e; } } $total_top = count( $top_level_elements ); if ( $paging ) { $this->max_pages = (int) ceil( $total_top / $per_page ); } else { $end = $total_top; } if ( ! empty( $args[0]['reverse_top_level'] ) ) { $top_level_elements = array_reverse( $top_level_elements ); $oldstart = $start;Changelog
Introduced in 2.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
...$args parameter by adding it to the function signature.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/class-wp-walker.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.