Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to use query_posts to list all descendants of a page ID. Using post_parent only lists the first-level children.

This Trac ticket seems to address it: http://core.trac.wordpress.org/ticket/5742, but I don't understand it -- post_parent only lists the first-level children.

<?php
    query_posts( array(
    'post_parent' => 58, // Only shows posts that are direct children of the Machinery page. I want all descendants.
    //'child_of' => 58, // When used, shows posts filterd by taxonomy and term, but not filtered by child of ID 58. Acutally omits direct child of 58 but shows grandchild.
    'post_status' => 'any',
    'post_type' => 'any',
    'taxonomy' => 'industries',
    'term' => 'dairy'
    )); 
?>
<?php if ( have_posts() ) : ?>
    <h4>Dairy Machinery</h4>
    <ul>
        <?php while (have_posts()) : the_post(); ?>    
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
        <?php endwhile; ?>
    </ul>
<?php endif; wp_reset_query(); ?>

How do I get query_posts to list all descendants of a page ID?

share|improve this question

1 Answer 1

If you don't have to use query_posts() then this is one method:

function my_menu() {
    global $post;

    if(!$post->post_parent){
        // will display the subpages of this top level page
        $children = wp_list_pages( array( 'title_li' => '', 'child_of' => $post->ID, 'echo' => 0 ) );
    } elseif($post->ancestors){
        // diplays only the subpages of parent level
        $ancestors = end($post->ancestors);
        $children = wp_list_pages( array( 'title_li' => '', 'child_of' => $ancestors, 'echo' => 0 ) );
    } else {
        // diplays all pages
        $children = wp_list_pages( array( 'title_li' => '', 'echo' => 0 ) );
    }

    if ($children) {
        echo '<ul id="my-menu">';
        echo $children;
        echo '</ul>';
    }

}
share|improve this answer
    
Thanks for the help. Your code shows the descendants of the current top-level page. What I need is the descendants of a specific page ID, then only show those pages that have a certain taxonomy term. Any ideas? I can get the pages like this: <?php $children = wp_list_pages("title_li=&child_of=58&echo=0"); ?> But then how do I filter them to show only the ones with: 'taxonomy' => 'industries', 'term' => 'dairy' –  mikedev Apr 13 '12 at 18:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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