Forums

query_posts() vs. get_posts() problem... (3 posts)

  1. IndigoDragon
    Member
    Posted 2 months ago #

    Hi all,

    I've got a weird problem with using the query_posts() vs. get_posts() core functions. The thing is that the query_posts() function gives me the desired children of the ID. Like:

    query_posts(array(
    'post_type' => 'page',
    'post_parent' => $optID
    ));

    But...the "post_parent" parameter shouldn't be an optional argument for the query_posts() function... (At least not according to the documentation.)

    While the get_posts() with the same arguments returns an empty 'query'.

    get_posts(array(
    'post_type' => 'page',
    'post_parent' => $optID
    ));

    Can anyone tell me what's going on...?
    Thanks for your reply!

  2. MichaelH
    moderator
    Posted 2 months ago #

    Quoting apljdi "query_posts() is meant to modify the main loop for the page, not for creating secondary loops"

    I have a parent page with two children. Those two children are both returned by the new WP_Query and get_posts:

    <?php
    $args = array(
    'showposts'=>-1,
    'caller_get_posts'=>1,
    'post_type' => 'page',
    'post_parent' => 93
    );
    $parent = new WP_Query($args);
    $parent2 = get_posts($args);
    
    echo "<pre>"; print_r($parent); echo "</pre>";
    echo "<pre>"; print_r($parent2); echo "</pre>";
    ?>
  3. Otto42
    Moderator
    Posted 2 months ago #

    "post_parent" is perfectly valid. Just not documented very well.

    I'm not sure why they'd give you different results though. There's not a lot of difference between them. get_posts suppresses filters on the query, and ignores sticky posts. That's about it, really.

Reply

You must log in to post.

About this Topic