get_page_children() not returning all child pages

I have in my WordPress theme, a section where I am getting child pages to display their information. This is what I have right now:

<?php 
                $my_wp_query = new WP_Query();
                $all_wp_pages = $my_wp_query->query(array('post_type' => 'page'));

                $staff = get_page_children(8, $all_wp_pages);

                foreach($staff as $s){
                    $page = $s->ID;
                    $page_data = get_page($page);
                    $content = $page_data->post_content;
                    $content = apply_filters('the_content',$content);
                    $content = str_replace(']]>', ']]>', $content);
                    echo '<div class="row-fluid"><span class="span4">'; 
                    echo get_the_post_thumbnail( $page ); 
                    echo '</span><span class="span8">'.$content.'</span></div>';
                } 
        ?>

I have five child pages that should be showing up, but only three are returning. I used print_r on $staff to see if the other pages were even in the array, but they aren’t. I’m not sure what the problem could be.

Related posts

Leave a Reply

5 comments

  1. There is nothing wrong with get_page_children() or new WP_Query(). By default WP_Query returns only the last x number of pages created. It’s the limit imposed on WP_Query.

    get_page_children() simply takes the pages array returned by WP_Query and filters the children pages from that list. According to WordPress Codex: get_page_children “…does not make any SQL queries to get the children.”

    To fix the issue simply use:

        $query = new WP_Query( 'posts_per_page=-1' );
    

    Your code with the fix:

        <?php 
        $my_wp_query = new WP_Query();
        $all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => -1));
    
        $staff = get_page_children(8, $all_wp_pages);
    
        foreach($staff as $s){
            $page = $s->ID;
            $page_data = get_page($page);
            $content = $page_data->post_content;
            $content = apply_filters('the_content',$content);
            $content = str_replace(']]>', ']]>', $content);
            echo '<div class="row-fluid"><span class="span4">'; 
            echo get_the_post_thumbnail( $page ); 
            echo '</span><span class="span8">'.$content.'</span></div>';
        } 
        ?>
    

    Here is a helper function that you can call whenever you need to get page children

        function my_get_page_children( $page_id, $post_type = 'page' ) {
            // Set up the objects needed
            $custom_wp_query = new WP_Query();
            $all_wp_pages    = $custom_wp_query->query( array( 'post_type' => $post_type, 'posts_per_page' => -1 ) );
    
            // Filter through all pages and find specified page's children
            $page_children = get_page_children( $page_id, $all_wp_pages );
    
            return $page_children;
        }
    

    Example

    You code with with the helper function

        foreach(my_get_page_children(8) as $s){
            $page = $s->ID;
            $page_data = get_page($page);
            $content = $page_data->post_content;
            $content = apply_filters('the_content',$content);
            $content = str_replace(']]>', ']]>', $content);
            echo '<div class="row-fluid"><span class="span4">'; 
            echo get_the_post_thumbnail( $page ); 
            echo '</span><span class="span8">'.$content.'</span></div>';
        } 
    
  2. I’ve had a similar problem – looks like get_page_children behaves weird… (in my case, for one page which had three children it returned three, for another with four it returned zero! – can’t work it out..)

    I got round it by using a custom query instead:

    $params = array(
    'post_type'=>'page',
    'post_parent'=> 8,
    );
    $staff = query_posts($params);
    

    Similar here: http://www.sanraul.com/2010/08/28/get-page-children/

    NOTE: depending on where you use this, you might need a wp_reset_query(); as well – or else that query_posts() could break your main loop!

    Hope that helps! – A

  3. $curID = get_the_ID();
    
    query_posts(
        array(
            'post_type'=>'page',
            'post_parent'=>$curID,
            'orderby' => 'menu_order',
            'order' => 'ASC'
        )
    );
    
    echo '<ul>';
    while ( have_posts() ) : the_post();
        echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    endwhile;
    echo '</ul>';
    
    wp_reset_query();
    
  4. SEE EDIT BELOW

    Having just experienced and solved this issue, I thought I’d share the approach I had to take.

    Originally, I used a new WP_Query to grab all pages and then fed the query result to get_page_children():

    $all_pages = new WP_Query( array( 'post_type' => 'page' ) );
    wp_reset_postdata();
    $page_id = get_the_ID();
    
    // pass $all_pages as the second parameter
    $page_children = get_page_children( $page_id, $all_pages );
    
    foreach ( $page_children as $childObj ) {
        echo '<a href="' . $childObj->guid . '">' . $childObj->post_title . '</a>'
    }
    

    For whatever reason, the above code did not work for me.

    Instead, I passed the ‘posts’ object of $all_pages as the second parameter, like so:

    $all_pages = new WP_Query( array( 'post_type' => 'page' ) );
    wp_reset_postdata();
    $page_id = get_the_ID();
    
    // pass the posts object of $all_pages as the second parameter
    $page_children = get_page_children( $page_id, $all_pages->posts );
    
    foreach ( $page_children as $childObj ) {
        echo '<a href="' . $childObj->guid . '">' . $childObj->post_title . '</a>';
    }
    

    I hope this helps someone!


    EDIT:
    After experiencing more trouble with the get_page_children() function, I choose a different route entirely that has been working consistently to achieve the same end result:

    $page = get_queried_object();
    $children = get_pages( 'child_of=' . $page->ID . '&parents=' . $page->ID );
    
    // loop through the child page links
    foreach ( $children as $child ) {
        echo '<a href="' . get_permalink( $child->ID ) . '">' . get_the_title( $child->ID ) . '</a>';
    }
    
  5. You can simply use

    $args = array(
            'post_parent' => $parent_id ,
            'post_type'   => 'page', 
            'numberposts' => -1,
            'post_status' => 'publish' 
        );
        $children = get_children( $args );
    

    $children variable will give you to all children pages of parent ID. if you are using WP_Query make sure to reset query after code for more info
    https://developer.wordpress.org/reference/functions/get_page_children/

    use this code in your functions.php file and call function with parent page id

        function load_clildren( $parent_id=null ){
        $parent_id  =   ( $parent_id ) ? $parent_id : $post->ID;
    
        $args = array(
            'post_parent' => $parent_id ,
            'post_type'   => 'page', 
            'numberposts' => -1,
            'post_status' => 'publish' 
        );
        $children = get_children( $args );
        return $children;
    }
    

    function call method

    $ch_pages   =   load_clildren( $parent_id );
    

    or
    using in parent page

    $ch_pages   =   load_clildren();