Get_pages (array ('child_of' => $ post-> ID) does not show all children

I'm new to Wordpress and wondering if anyone can shed any light on this code. I am trying to list all the auxiliary pages on their parent page, here is the code with some html shared:

<?php
$mypages = get_pages( array( 'child_of' => $post->ID ) );

foreach( $mypages as $page ) {      
    $content = $page->post_content;
    if ( ! $content ) // Check for empty page
        continue;

    $content = apply_filters( 'the_content', $content );
?>

<p style="color: white; text-transform: uppercase;"><?php echo $page->post_title; ?></p>

<?php
   }   
?>

The code works, and the correct subpages are displayed - but not all of them. The 7 oldest posts are displayed, but not one of the newest pages I created this week. I checked and double checked that all the new and old pages are the same in every respect - the same template, the same parent page, the same creator, the same order number and all published ones. Does anyone have an idea of ​​what I can do wrong?

+4
4

:

$args = array('child_of' => $post->ID,'sort_order' => 'desc',
'sort_column' => 'ID',
);
+1

child_of false. , true .

<?php
    $mypages = get_pages( array( 'parent' => $post->ID,  'hierarchical' => 0  ) );
?>

: https://codex.wordpress.org/Function_Reference/get_pages

0

, , , .

$args = array( 
        'child_of' => $post->ID, 
        'parent ' => $post->ID,
        'hierarchical' => 0,
        'sort_column' => 'menu_order', 
        'sort_order' => 'asc'
);
$mypages = get_pages( $args );

wordpress Doc .

, 'sort_column' => 'menu_order', 'sort_column' => 'post_date',.

, .

<?php

$args = array(
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'post_parent'    => $post->ID,
    'order'          => 'ASC',
    'orderby'        => 'menu_order'
 );

$mypages = new WP_Query( $args );


if ( $mypages->have_posts() ) : ?>

    <?php while ( $mypages->have_posts() ) : $mypages->the_post(); ?>

        <p style="color: white; text-transform: uppercase;"><?php the_title(); ?></p>

    <?php endwhile; ?>

<?php endif; wp_reset_query(); ?>

You can also use wp_list_pages to render direct HTML.

0
source

I fixed the problem, but actually I have no idea why this works. Any comments on why the trick did this would be awesome.

From my source code, I removed the "if (! $ Content)" section, and they all appear even if all pages have the same amount of content.

So at the end, my code reads:

<?php
$mypages = get_pages( array( 'child_of' => $post->ID ) );

foreach( $mypages as $page ) {      
$content = $page->post_content;
$content = apply_filters( 'the_content', $content );
?>

<p style="color: white; text-transform: uppercase;"><?php echo $page->post_title; ?></p>

<?php
}   
?>

I never had to change the parameters of the get_pages function.

0
source

Source: https://habr.com/ru/post/1669613/


All Articles