get_page_children()WP 1.5.1

Собирает дочерние страницы из переданного массива страниц. Обрабатываются все уровни вложенности.

Функция работает как фильтр и не делает никаких запросов к БД. Фильтрует массив из второго параметра: находит в нём все дочерние страницы к указанному ному $page_id. При фильтрации учитываются все уровни вложенности.

Хуков нет.

Возвращает

WP_Post[]. Массив объектов WP_Post.

Использование

get_page_children( $page_id, $pages );
$page_id(число) (обязательный)
ID страницы, дочерние к которой нужно получить.
$pages(массив) (обязательный)
Список объектов постов, по которым будет проходить поиск дочерних страниц.

Примеры

0

#1 Пример использования функции:

// Получаем все страницы, по которым будет проходить поиск.
$all_pages = ( new WP_Query() )->query( [ 
	'post_type' => 'page', 
	'posts_per_page' => -1 
] );

// страница, дочерние которой нужно получить
$about_id = 7;

// оставим только дочерние к Portfolio
$about_childrens = get_page_children( $about_id, $all_pages );

// упростим вывод
foreach( $about_childrens as & $page ){
	unset( 
		$page->post_content, 
		$page->post_date_gmt, 
		$page->post_password, 
		$page->post_modified_gmt, 
		$page->post_content_filtered, 
		$page->post_mime_type
	);
}

// результат
print_r( $about_childrens );

Получим:

Array
(
	[0] => WP_Post Object
		(
			[ID] => 10124
			[post_author] => 12
			[post_date] => 2018-05-19 08:11:19
			[post_title] => Политика конфиденциальности
			[post_excerpt] => 
			[post_status] => publish
			[comment_status] => closed
			[ping_status] => closed
			[post_name] => privacy-policy
			[to_ping] => 
			[pinged] => 
			[post_modified] => 2018-09-12 05:28:35
			[post_parent] => 7
			[guid] => /about/privacy-policy
			[menu_order] => 3
			[post_type] => page
			[comment_count] => 0
			[filter] => raw
		)

	[1] => WP_Post Object
		(
			[ID] => 9976
			[post_author] => 12
			[post_date] => 2018-05-05 01:01:09
			[post_title] => Активные пользователи
			[post_excerpt] => 
			[post_status] => publish
			[comment_status] => closed
			[ping_status] => open
			[post_name] => best-users
			[to_ping] => 
			[pinged] => 
			[post_modified] => 2018-05-05 02:04:25
			[post_parent] => 7
			[guid] => /about/best-users
			[menu_order] => 2
			[post_type] => page
			[comment_count] => 0
			[filter] => raw
		)

	[2] => WP_Post Object
		(
			[ID] => 3591
			[post_author] => 12
			[post_date] => 2013-10-30 19:26:40
			[post_title] => О перепечатке
			[post_excerpt] => 
			[post_status] => publish
			[comment_status] => closed
			[ping_status] => open
			[post_name] => o-perepechatke
			[to_ping] => 
			[pinged] => 
			[post_modified] => 2018-05-05 02:32:15
			[post_parent] => 7
			[guid] => /about/o-perepechatke
			[menu_order] => 4
			[post_type] => page
			[comment_count] => 0
			[filter] => raw
		)

	[3] => WP_Post Object
		(
			[ID] => 25
			[post_author] => 12
			[post_date] => 2010-04-05 04:24:04
			[post_title] => Контакты
			[post_excerpt] => 
			[post_status] => publish
			[comment_status] => closed
			[ping_status] => open
			[post_name] => contacts
			[to_ping] => 
			[pinged] => 
			[post_modified] => 2018-05-05 02:34:09
			[post_parent] => 7
			[guid] => /about/contacts
			[menu_order] => 1
			[post_type] => page
			[comment_count] => 0
			[filter] => raw
		)

)

Список изменений

С версии 1.5.1 Введена.

Код get_page_children() WP 6.4.3

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;
}