Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_page_children() not returning all child pages

Tags:

php

wordpress

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.

like image 983
jordan Avatar asked Jan 08 '13 21:01

jordan


2 Answers

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>';
    } 
like image 106
Zeeshan Avatar answered Nov 03 '22 19:11

Zeeshan


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

like image 41
Adam Marshall Avatar answered Nov 03 '22 20:11

Adam Marshall