questions

WordPress show (grand)parent, child and grandchildren pages

Rate this post

I have a brand page (as parent) with several products (children), product groups (also children) with products (grand children) as pages. I’m querying a lot which seems a bit ridiculous. Is there a better way to achieve this the following?

Brand (parent page)

  • Product (child page)
  • Product group (parent page)
    • Product (child page)
    • Product group (parent page)
      • Product (child)

Code I’m using to query the pages:

<?php

$args = array(
'cat' => 54,
'orderby' => 'menu_order',
'order' => 'ASC',
'hierarchical' => 1,
'post_parent' => $post->ID,
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {

  while ( $the_query->have_posts() ) {
      $the_query->the_post();

      get_template_part( 'strips/card-product' );
  }

} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();


$args = array(
'category__not_in' => 54,
'order' => 'ASC',
'orderby' => 'menu_order',
'hierarchical' => 1,
'post_parent__in' => array($post->ID),
'parent' => -1,
'offset' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {


    while ( $the_query->have_posts() ) {
        $the_query->the_post();


    // INSIDE PRODUCT GROUP
    if ( in_category('product-groep') ) {

      $args = array(
      'category__not_in' => 54,
      'order' => 'ASC',
      'orderby' => 'menu_order',
      'hierarchical' => 1,
      'post_parent' => get_the_id(),
      'parent' => -1,
      'offset' => 0,
      'post_type' => 'page',
      'post_status' => 'publish'
      );

  // The Query
  $query1 = new WP_Query( $args );

  // The Loop
  while ( $query1->have_posts() ) {
      $query1->the_post();


        if ( in_category('product-groep') ) {

        $args = array(
        'category__not_in' => array(54,8),
        'order' => 'ASC',
        'orderby' => 'menu_order',
        'hierarchical' => 1,
        'post_parent' => get_the_id(),
        'parent' => -1,
        'offset' => 0,
        'post_type' => 'page',
        'post_status' => 'publish',
        'post__not_in' => array(692)
        );

        // The Query
        $query2 = new WP_Query( $args );

        // The Loop
        while ( $query2->have_posts() ) {
            $query2->the_post();

            get_template_part( 'strips/card-product' );


              }

              wp_reset_postdata();

            } else
          {

          } 
        }

        wp_reset_postdata();

        // OUTSIDE PRODUCT GROUP
                                        }
                                        else {


                                        }
        ?>

        <!-- END LOOP 1 -->

      <?


                      }

                  } else {
                      // no posts found
                  }
                  /* Restore original Post Data */
                  wp_reset_postdata();
                  ?>

 

✔️Solution:

Use this WP function get_pages() and get_page_children()

function get_child_pages( $parent_page_ID ){
    $all_pages = get_pages( array( 'post_type'=> 'page' ) );
    $child_pages = get_page_children( $parent_page_ID, $all_pages );
    if( !empty( $child_pages ) ){
        $html .= '<ul>';
        foreach ( $child_pages as $key => $child_page ) {
            $html .= '<li>'.$child_page->post_title;
            get_child_pages( $child_page->ID );
            $html .= '</li>';
        }
        $html .= '</ul>';
    }
    return $html;
}

function list_pages(){
    $html = '';
    $parent_pages = get_pages( array( 'parent' => 0, 'post_type'=> 'page' ) );
    $html.= '<ul>';
    foreach ( $parent_pages as $parent_page ) {
        $html .= '<li>'.$parent_page->post_title;
        $html .= get_child_pages( $parent_page->ID );
        $html .= '</li>';
    }
    $html.= '</ul>';
    return $html;
}
add_shortcode( 'list_pages', 'list_pages' );

Leave a Reply

Your email address will not be published.

Back to top button