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. Haim

    thanks guys for the quick response,
    i manage to do the paging, my solution looks allot like what Derek suggested

  2. Hello people,

    I need this thing but i cannot understand how to use it. i installed the plugin but i just don’t know what to do with it…

    say i want main page “Articles” to display all its sub-pages (articles categories) and for each sub-page to display the relevant list of articles – like this:

    Articles

    Category 1
    article 1
    article 2
    article 3

    Category 2
    article 1
    article 2
    article 3

    where am i supposed to place this code then? and is it capable of doing what i need?

    also would it be possible to place an icon next to each category?

    thanks 🙂

  3. Karl

    @findigart,

    I think you’re after the same thing as me, wanting to output child pages together with their grandchild pages. Still not sure how to do this.

    I’ve had another try with the code Westi published above, bending it this way and that. The closest I can get is that I can output a list similar to:

    CHILD 1
    Child 1
    Child 2
    Child 3

    CHILD 2
    Child 1
    Child 2
    Child 3

    CHILD 3
    Child 1
    Child 2
    Child 3

    It’s as if the child_of in the inner_query is not being used. Whatever I put in that child_of doesn’t seem to make a difference.

  4. Peter,

    One word: “Brilliant”. Thank you for this. Surely this should should ship out of the box? When you mix this with custom fields you can really achieve some great results!

    Thanks again.

    Joe

    1. @Stephen: Yes you could use post_parent instead. Looking back it looks like that was added back in WordPress 2.6.

  5. Vishal Rao

    Excellent plugin! I have used this on my home page to retrieve “x” recent Pages and it works great, which I couldn’t from other methods. Here’s my code on the home page:

    query_posts(‘static=true&post_status=publish&orderby=date&order=DESC&child_limit=3’)

    Is this the right way?

    Also, can you please tell me if static=true argument is specific to your plugin, because the wordpress document on query_posts states page_type=page for Pages.

    1. @Vishal: You can use post_type=page instead of the static=true if you want either will work. static was how pages were referred to when I originally wrote the plugin.

  6. Hi Peter!

    This is a great plugin! Really… I find it very udeful. Though I’m struggling to get the output shuffled.

    I mean, maybe output 4 children but ordered randomly. Let’s say a page with 30 children. I would love to be able to get 4 randomly chosen children.

    I’ve tried the orderby=RAND, but that doesn’t seem to have any effect.

  7. Kevin

    You talk about showing grandchildren but it doesn’t seem to really do that. I want to have a showing ALL items and sub items. So children of children don’t show, how do i get them to show?

Comments are closed.