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.
There is nothing wrong with
get_page_children()
ornew WP_Query()
. By defaultWP_Query
returns only the lastx
number of pages created. It’s the limit imposed onWP_Query
.get_page_children()
simply takes the pages array returned byWP_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:
Your code with the fix:
Here is a helper function that you can call whenever you need to get page children
Example
You code with with the helper function
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:
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 thatquery_posts()
could break your main loop!Hope that helps! – A
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():
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:
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:
You can simply use
$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 call method
or
using in parent page