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. @Alice: I suspect you need to be more specific in your call to query_posts() to ensure you only get published pages. You need to add ‘post_status=publish’ to the argument list.

  2. Anders

    This is just what i’ve been looking for. I will try it out when i get home from work tonight.
    By the way, is there a way to also, apart from the excerpt and page title, display the page’s meta data. Something like this:

    May 15th, 2008 – 12:00 pm
    Page title (link to the page)
    This is the page’s excerpt

  3. naires

    hi!

    nice plugin, great work! there is a way to sort pages by parameter: sort_column=menu_order ?

    thanks!

  4. Adam

    I can’t seem to get this working. I’m trying “query_posts('child_of=2');” and its pulling back 0 results. But if I use “get_pages('child_of=2');” I get 6 results (the expected amount). I know I could use “get_pages” but I really would like to use “The Loop” for the work I’m doing.

  5. @Adam: You need more arguments in your query to get it to work. Try something like: query_posts('static=true&posts_per_page=-1&child_of=2');

  6. Adriel R

    Hi Peter,

    Great plugin. How do I limit the # of children being displayed? The conventional showposts=# is not working. Any help would be great. Thanks.

  7. @Adriel: If my memory serves me correctly, you need to use post_per_page= to limit the number of posts returned.

  8. James

    hello, would you mind helping me? I am not sure how to use this plugin. Do I need to put code within my template code? On my ‘hub’ Pages, I have a brief passage of text as an introduction. this introduction is my Page content and then I would like to list the child pages below.

    thank you

  9. james

    hello, I’ve gathered I have to add the code above (or equivalent) to my template but as it is using query_posts() am I correct in thinking that I need to place this outside of the loop?

Comments are closed.