$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - WP_Query returns different results from get_posts()|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - WP_Query returns different results from get_posts()

matteradmin8PV0评论
Closed. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 6 years ago.

Improve this question

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. This question is off-topic. It is not currently accepting answers.

Questions that are too localized (such as syntax errors, code with restricted access, hacked sites, hosting or support issues) are not in scope. See how do I ask a good question?

Closed 6 years ago.

Improve this question

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!

Share Improve this question asked Feb 21, 2019 at 23:05 Aneesh BarthakurAneesh Barthakur 31 bronze badge 3
  • Note that get_posts calls WP_Query internally – Tom J Nowell Commented Feb 21, 2019 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 Commented Feb 21, 2019 at 23:54
  • @czerspalace you should post that as an answer – Tom J Nowell Commented Feb 22, 2019 at 1:48
Add a comment  | 

3 Answers 3

Reset to default 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);

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.

In your WP_Query code instead of

echo $post->post_title;     

Try

echo $child_pages_query->post->post_title;
Post a comment

comment list (0)

  1. No comments so far