0

So I have two code snippets, trying to get the child pages of the current page. The current page is "Sample", and the following code lives in page-sample.php.

The first snippet uses get_posts()

$args = array(
                'post_parent' => $post->ID,
                'post_type'   => 'page', 
                'post_status' => 'any' 
            );
$children = get_posts( $args );
// $children = get_children( $args ); //this also works,btw

foreach($children as $child){
    setup_postdata( $child );
    echo "<h1>" .  $child->post_title . "</h1>";
}

This correctly displays the child posts.

The second snippet uses a new WP_Query object

$args = array(
                'post_parent' => $post->ID,
                'post_type'   => 'page', 
                // 'numberposts' => -1,
                'post_status' => 'any' 
            );
$child_pages_query= new WP_Query(args);
// echo $child_pages_query->post_count; // wrong answer already!
if ($child_pages_query->have_posts()){
    while($child_pages_query->have_posts()){
        $child_pages_query->the_post();
        echo "<h1> " . $post->post_title . " </h1>";
        }
}

This incorrectly displays all posts . No pages, just posts.

I really can't figure out what I'm doing wrong

  1. No plugins are installed
  2. I commented out all parent theme code, including header,footer, till it was a bare html page, even though I couldn't see anything in header.php or footer.php that could be affecting this.

Now the funny part is changing the WP_Query args doesn't seem to be making any difference. Always, all posts. WP_Query works on other pages.

Any help would be appreciated. Much thanks in advance!

closed as off-topic by fuxia Feb 26 at 16:45

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Note that get_posts calls WP_Query internally – Tom J Nowell Feb 21 at 23:39
  • 2
    You are missing a $ before args in $child_pages_query= new WP_Query(args);. It should be $child_pages_query= new WP_Query($args);. Have you enabled WP_DEBUG? – czerspalace Feb 21 at 23:54
  • @czerspalace you should post that as an answer – Tom J Nowell Feb 22 at 1:48
3

You are missing a $ before args in

$child_pages_query= new WP_Query(args); 

It should be

$child_pages_query= new WP_Query($args);
  • Oh my, I can't believe I spent so many hours trying to figure this out -_- . You're right of course, and the query is working fine now. Much thanks! But why doesn't php throw an error for this one? Does "args" without the $ also mean something? – Aneesh Barthakur Feb 22 at 6:45
  • Do you have WP_DEBUG enabled? If you enable you should see the error – czerspalace Feb 22 at 7:36
  • It would throw a warning/notice not an error – Tom J Nowell Feb 22 at 15:22
  • No I don't. This would be my first wordpress/php project. I will definitely check it out now. – Aneesh Barthakur Feb 24 at 10:33
1

You forgot the $ on your WP_Query args array:

$child_pages_query= new WP_Query(args);

Should be:

$child_pages_query= new WP_Query($args);

This should show up as a PHP warnings/notice in the error logs, and is preventable with standard debugging and development practices.

-3

In your WP_Query code instead of

echo $post->post_title;     

Try

echo $child_pages_query->post->post_title;

Not the answer you're looking for? Browse other questions tagged or ask your own question.