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. @james: Yes you would need to place this second loop outside of the normal loop. I am working on a better way of doing this any hope to release a improved method soon – hopefully the improved method will allow you to do what you want without adding anything to your template.

  2. james

    Sorry to bother you again, would it be at all possible, when I have done it, to show you my template to see if I have coded it correctly in terms of the loops etc?

  3. Tom

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

    Hello Peter, also this doesn’t seem to work for me! The loop returns all child pages instead of, for example, two pages.
    I’m using the following code:
    query_posts('static=true&posts_per_page=2&child_of=7&orderby=menu_order&order=ASC');

    Any idea? Thanks!

  4. Happened to me too. I used query_posts(‘static=true&posts_per_page=1&child_of=’.$id.’&order=ASC’);
    and is printing all childs, not only one.

  5. KandB

    we also had the same problem where drafts and unpulished items were being displayed, so thanks to alice for the question and westi for the answer as this php query_posts(‘post_status=publish&static=true&posts_per_page=0&child_of=’.$id.’&order=ASC’) worked perfectly

    cheers for the awesome plugin!

  6. KandB

    Hi Everyone, we were wanting to get a grided page we created to have an order the same as our drop down menu and side bar and this worked for us
    php query_posts(‘post_status=publish&static=true&posts_per_page=0&child_of=’.$id.’&order=ASC&orderby=menu_order’)

  7. Paul

    Hi Westi, thanks for the great plugin. Like the people above, I am having trouble limiting the amount of posts shown on my page – basically it doesn’t work. Has anyone come up with a solution for this?

    Here is the code:

    The posts_for_page doesn’t work, it just shows all. I tried show_posts, but that didn’t work either.

    Thanks

  8. Hmm. I had assumed that posts_for_page would work. I will have to dig into this as I can reproduce the fact that posts_per_page has no affect on the query here.

Comments are closed.