Parent and Child Pages in Bricks

A user asked:

Hi there, I have a Toggle added to the pages using ACF. I have a pages where I Loop all the pages that have that toggle ON. Easy.

Some of those pages have children. I want to create another page that loops all those children. So, I need all the children of pages that toggle on.

Any idea how to do it?

This Pro tutorial for Bricks users shows how we can

  • list all the Pages as an unordered list with indentation
  • output all the Pages that have the value of a custom field of true/false type set to true i.e., featured Pages
  • output all featured Pages that have at least 1 child i.e., parent Pages in ascending order of titles
  • output all child Pages of the featured parent Pages in ascending order of titles

Step 1

To list all the Pages in a hierarchical manner, add this in a Code element:

<?php

wp_list_pages(
  [
  	'title_li' => '',
  ]
);

?>

Step 2

To output all Pages that have a true (1) or false (0) type of custom field (featured in this example) equal to 1, set up a query loop like this:

Step 3

To output all parent featured Pages ordered by titles in ascending order, enable query loop on a block.

Post type: Pages

Enable ‘Query editor (PHP)’.

Paste:

$args = [
  'post_type' => 'page',
  'posts_per_page' => 100, // a large number to ensure all are returned
  'meta_query' => [
    [
      'key' => 'featured',
      'value' => '1',
    ]
  ],
  'fields' => 'ids',
];

$featured_pages = new WP_Query( $args );

// Filter the above to only those that have at least 1 child Page
$parent_featured_pages = [];
foreach ( $featured_pages->posts as $post_id ) {
  $children = get_children( [
    'post_parent' => $post_id,
    'post_type' => 'page',
    'posts_per_page' => 1,
  ] );
  if ( ! empty( $children ) ) {
    $parent_featured_pages[$post_id] = get_the_title( $post_id );
  }
}

// Sort the pages by title in ascending order
asort( $parent_featured_pages );

return [
  'post__in' => array_keys( $parent_featured_pages ),
  'orderby' => 'post__in',
];

Replace featured with the name/ID of your custom field.

Step 4

To output children of all parent featured Pages ordered by titles in ascending order, enable query loop on a block.

Post type: Pages

Enable ‘Query editor (PHP)’.

Paste:

$args = [
  'post_type' => 'page',
  'posts_per_page' => 100, // a large number to ensure all are returned
  'meta_query' => [
    [
      'key' => 'featured',
      'value' => '1',
    ]
  ],
  'fields' => 'ids',
];

$featured_pages = new WP_Query( $args );

$child_pages_with_titles = [];
foreach ( $featured_pages->posts as $post_id ) {
  $children = get_children( [
    'post_parent' => $post_id,
    'post_type' => 'page',
    'posts_per_page' => 100, // a large number to ensure all are returned
  ] );
  if ( ! empty( $children ) ) {
    foreach ( array_keys( $children ) as $child_id ) {
      // Store the title with the child ID for sorting
      $child_pages_with_titles[$child_id] = get_the_title( $child_id );
    }
  }
}

// Sort child pages by title
asort( $child_pages_with_titles );

// Extract the sorted child page IDs
$sorted_child_page_ids = array_keys( $child_pages_with_titles );

return [
  'post__in' => $sorted_child_page_ids,
	'orderby' => 'post__in',
];

Replace featured with the name/ID of your custom field.