treehouse : what would you like to learn today?
Web Design Web Development iOS Development

how to build a 'post_parent' array?

  • Hey guys! been a while...

    I'm trying to get the following line to work for WP:

    query_posts (array('showposts' =>100, 'post_type' =>'page', 'post_parent' => '80, 73, 67' ));


    The page returns with a list of all pages rather than just the child pages of pages 80, 73 and 67 - so clearly my syntax is wrong. I've tried various combinations of if's and and's but it either comes back the same or not at all. What would the correct way of doing this be?


    Thanks in advance!
    Éamonn
  • Clearly a page can only have one (direct) parent, so the syntax is invalid - you must use 'post_parent' => 80.

    There are ways to query for a number of categories, tags or posts e.g. 'post__in' => array(1,5,7,89,102) will return all the posts with ids specified in the array, however there is no way to perform the same query based on the post parent.

    The simplest way to achieve this would appear to be through using a filter.

    <?php
    // place the following in your themes functions.php
    function post_parent_in($where = '') {
    $where .= \" AND post_parent IN (80, 73)\";
    return $where;
    }

    // use the following in your template
    add_filter('posts_where', 'post_parent_in');
    query_posts( array('showposts' => 100, 'post_type' =>'page', 'post_status' => 'publish') );
    ?>
  • Ta for that - sorry for the late response - I got bloody Swine Flu (not badly)!

    Given that I've already used the post_parent tag (with a different, single, value) twice already elsewhere on the same site, does changing the functions.php file not affect those other instances also? All these 'posts' are, in fact, pages so I can't categorise them. Otherwise the 'post_in' tag would have been perfect...

    Thanks!
  • Stay away from the apple sauce then - :lol:

    If you call add_filter, then query_posts then immediately
    remove_filter('posts_where', 'post_parent_in');

    it will only affect that one query_posts call. Hope this answers your question.

    Cheers,
    Dave
  • Will do :D

    Ta again! I'll give it a go.
  • Thanks! This was just what i was looking for.
  • Thanks!

    Is there a way to get paged results?