Bricks Query Type: Child Pages of the Current Page
Adds a "Child pages" query type to Bricks query loops that lists the subpages of the page being viewed, in menu order, for section overview pages.
Opens WPIDE on
Code
add_filter( 'bricks/setup/control_options', function ( $options ) {
$options['queryTypes']['wpide_child_pages'] = 'Child pages';
return $options;
} );
add_filter( 'bricks/query/run', function ( $results, $query_obj ) {
if ( ! is_object( $query_obj ) || 'wpide_child_pages' !== $query_obj->object_type ) {
return $results;
}
$parent = get_queried_object_id();
if ( ! $parent ) {
return array();
}
return get_posts( array(
'post_type' => get_post_type( $parent ),
'post_parent' => $parent,
'posts_per_page' => 50,
'orderby' => array( 'menu_order' => 'ASC', 'title' => 'ASC' ),
'no_found_rows' => true,
) );
}, 10, 2 );
add_filter( 'bricks/query/loop_object', function ( $loop_object, $loop_key, $query_obj ) {
if ( is_object( $query_obj ) && 'wpide_child_pages' === $query_obj->object_type && $loop_object instanceof WP_Post ) {
global $post;
$post = $loop_object; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
setup_postdata( $post );
}
return $loop_object;
}, 10, 3 );
Notes
Choose "Child pages" as the query type on any looping element; dynamic data like {post_title} works inside it.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.