函数文档

get_page_children()

💡 云策文档标注

概述

get_page_children() 函数用于从页面对象列表中识别给定页面 ID 的所有后代页面。它通过递归方式处理,无需数据库查询,直接基于传入的页面数组进行操作。

关键要点

  • 函数接受两个参数:$page_id(整数,必需)指定父页面 ID,$pages(WP_Post[] 数组,必需)提供页面对象列表。
  • 返回值为 WP_Post[] 数组,包含所有后代页面对象,顺序基于原始 $pages 数组。
  • 函数内部使用递归算法,通过构建 ID 到子页面的哈希映射来高效查找后代。
  • 适用于任何分层文章类型(如页面或自定义文章类型),不限于默认页面。

代码示例

// 获取所有页面
$all_wp_pages = get_pages();

// 获取特定页面(例如标题为 'Portfolio' 的页面)
$portfolio = get_page_by_title('Portfolio');

// 使用 get_page_children 查找后代
$portfolio_children = get_page_children($portfolio->ID, $all_wp_pages);

// 输出结果
echo '' . print_r($portfolio_children, true) . '';

注意事项

  • 函数不执行数据库查询,依赖传入的 $pages 数组,确保数组包含所有相关页面以避免遗漏后代。
  • 对于自定义分层文章类型,需使用 get_pages() 或类似函数获取对象列表,并正确设置参数如 post_type。
  • 递归调用可能导致性能问题,如果页面层级很深或数据量很大,建议测试优化。

📄 原文内容

Identifies descendants of a given page ID in a list of page objects.

Description

Descendants are identified from the $pages array passed to the function. No database queries are performed.

Parameters

$page_idintrequired
Page ID.
$pagesWP_Post[]required
List of page objects from which descendants should be identified.

Return

WP_Post[] List of page children.

More Information

This function calls itself recursively.

Source

function get_page_children( $page_id, $pages ) {
	// Build a hash of ID -> children.
	$children = array();
	foreach ( (array) $pages as $page ) {
		$children[ (int) $page->post_parent ][] = $page;
	}

	$page_list = array();

	// Start the search by looking at immediate children.
	if ( isset( $children[ $page_id ] ) ) {
		// Always start at the end of the stack in order to preserve original `$pages` order.
		$to_look = array_reverse( $children[ $page_id ] );

		while ( $to_look ) {
			$p           = array_pop( $to_look );
			$page_list[] = $p;
			if ( isset( $children[ $p->ID ] ) ) {
				foreach ( array_reverse( $children[ $p->ID ] ) as $child ) {
					// Append to the `$to_look` stack to descend the tree.
					$to_look[] = $child;
				}
			}
		}
	}

	return $page_list;
}

Changelog

Version Description
1.5.1 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    Examples

    query(array('post_type' => 'page'));
    
    // Get the page as an Object
    $portfolio =  get_page_by_title('Portfolio');
    
    // Filter through all pages and find Portfolio's children
    $portfolio_children = get_page_children( $portfolio->ID, $all_wp_pages );
    
    // echo what we get back from WP to the browser
    echo '<pre>' . print_r( $portfolio_children, true ) . '</pre>';
    ?>

  2. Skip to note 4 content

    In one of my Hierarchical Custom Post Type (locations) I did @bhlarsen’s method, and in some extent it’s returning false children. So I did it my way:

    ID;
    
    //Instead of calling and passing query parameter differently, we're doing it exclusively
    $all_locations = get_pages( array(
                            'post_type'         => 'locations', //here's my CPT
                            'post_status'       => array( 'publish', 'pending' ) //my custom choice
                        ) );
    
    //Using the function
    $inherited_locations = get_page_children( $location_parent_id, $all_locations );
    
    // echo what we get back from WP to the browser (@bhlarsen's part :) )
    echo '' . print_r( $inherited_locations, true ) . '';
    ?>

    It’s giving me the correct children.