Query Child Of $Page

Current Release: 1.10 (download) – Released on May 26th 2008

What it does

This plugin allows you to run loops within your WordPress templates where you query for children of the current page.

How it works

The plugin adds an extra arguments to the list of arguments supported by query_posts().

The query argument `child_of` is used to add a WHERE to the database query to limit the pages returned to those with a post_page equal to the argument provided.
The query argument `child_limit` is used to limit the number of pages returned.
The query argument `child_offset` is used to offset the limiting to allow for pagination if required.

Requirements

This plugin has been tested with WordPress v2.8-beta and should work with all WordPress versions that support pages (v2.0 and later).

Usage Example

The following code is an example of what you could do to generate a list of the first 10 child pages from a parent page:

<div id="children">
<dl><?php query_posts('static=true&child_limit=10&child_of='.$id.'&order=ASC'); ?>
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<dt><a href="<?php the_permalink();?>"><?php the_title();?>:</a><em><?php the_excerpt(); ?></em></dt>
<?php endwhile; endif; ?>
</dl></div>

More detailed usage examples can be found in this article “Searching for Children and Grandchildren

Download

The latest version of the plugin may be downloaded here: pjw-query-child-of.1.10.zip

97 thoughts on “Query Child Of $Page

  1. Hi Peter,

    This plugin seems to be exactly what I want however I can’t get it to function properly.

    I want to call all the subpages of my portfolio page, loop through them and display some custom field data.

    for this I am using the following code

    <?php query_posts('child_of=44'); while (have_posts()) : the_post(); ?>
    
    <?php the_title() ?>
    
    <img src="?php echo get_post_meta($post->ID, "screenshot", true); ?>" />
    
    <?php echo get_post_meta($post->ID, "excerpt", true); ?>
    
    <a href="?php echo get_post_meta($post->ID, "url", true); ?>">Visit the site
    
    <a href="?php the_permalink() ?>">Read the case study »
    
    <?php endwhile; ?>
    

    The problem is that this is simply displaying literally everything in my blog, including posts, pages, uploaded files, basically everything with an ID.

    Could this be that this plugin doesn’t work with WordPress 2.2.2 (the version I am using).

    I really need this functionality and if it comes with it I will pay.

    Thanks, Tom

  2. Tom: It definetely works with 2.2.2 (as that is what I am currently running and it powers my WordPress page).

    This is what I have in my template file for that page:

    <?php query_posts('static=true&posts_per_page=-1&child_of='.$id.'&order=ASC'); ?>
            <?php if(have_posts()) : while (have_posts()) : the_post(); ?>
            <dt><a href="<?php the_permalink();?>"><?php the_title();?>:</a></dt>
                <dd style=""><em><?php the_excerpt(); ?></em></dd>
            <?php endwhile; endif; ?>
    

    Try updating your query to add those few extra arguments and see if that helps.

    Otherwise let me know and I’ll look into it some more.

  3. Hi Peter,

    Thanks for quick reply, have tried using your code and all its variations, still no luck, if I simply copy the code that you have posted then the page shows a list of everything on the site,

    I have no extra plugins running.

    Could you perhaps send me all the code from the template so that I can compare it all.

    Tom

  4. Is there a way I can have it show children of “current page”? I want to use this in a theme template that will be used for multiple pages (essentially I want to create a “section index” template).

    This combined with you “Page Excerpt” plugin is a powerful combination. Outstanding! 🙂

  5. Hi there!

    I have the same problem as Tom. The plugin doesn’t seem to work. I take two guesses. I’m querying for child pages of a child page… maybe it messes the query up?? Also it works differently when you have two queries on the same template.

    Please, let me know if you found the answer, since I really find your plugin a great idea!

  6. Alicia: What parameters are you including in the call to query_posts()? You need to have the static=true as well as the child_of argument as this tells WordPress that you are asking for Pages.

  7. Hi!
    This is my query:

    But it shows everything that’s inside my blog (including non-static pages) 🙁
    My page’s structure is this:

    — Level 1
    — Level 2
    ———- Sublevel 1
    ———- Sublevel 2 <– This is the page I’m building the template for
    ——————– Sub-sublevel 3 <– These two sub-subpages is what I want to show on the template
    ——————–Sub-sublevel 4

    Thanks for your time!

  8. Peter —

    I’m still not able to get this to work. I’ve tried exactly the example code you’ve shown us, plus I’ve tried hard-coding in a page ID. Same result. I’ve pared it down to query_posts('static=true&child_of=7'); and it still returns everything in my blog.

    Has anyone else gotten this to work? There are commenters saying this exact problem, and I don’t see anyone coming back to say “It worked!”

    I’m on wp-hackers. I would love to get this to work — if you’d like me to work with you to get this working, I’m game. 😉

    Personally I think this functionality (and page excerpts — that plugin works great thanks!) should be in core.

Comments are closed.