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

    Hi,
    Great plugin! Thanks!
    I used the following code to get it to work:
    php query_posts(“post_status=publish&static=true&posts_per_page=0&child_of=”.$post->ID.”&order=ASC&orderby=menu_order”)
    The child_of=”.$id.” somehow did not work at all.
    I still can’t get posts_per_page to work, but not worried about it, since I want all child pages under that page to show.
    I am using WP 2.6.2

  2. Ashish

    Hi Alice,
    Yes I tried ‘.$id.’ too, but it kept showing me all the parent and child pages on the site – that too in a weird fashion – Parent1, Child1-1, Parent2, Child 1-2, Child 1-3 and so on. In the end, it even displayed the form labels I made with cformsII.
    However ‘.$post->ID.’ works just fine. I am not really aware of the difference between the two, but with 2.6.x, the latter seems to work.

  3. DeeMo

    very strange. It “kinda” works for me: I do get a list of subpages with excerpt BUT after that, it’s also giving me the title and the content of the first subpage on the list.
    No apparent difference between the pages (read; thats not an unpublished page).
    Dang, this was almost doing it for me after searching the whole wp-sphere. Any Idea?

    I aiming aiming for this :
    post_parent)
    $children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&exclude='.$post->ID.'&echo=0'); else
    $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
    if ($children) { ?>


    but I need each child to display its excerpt

    Any idea on how to get your method to show the list even on subpages?

  4. DeeMo

    Sorry, part of the code went missing. here’s the correct version:

    if($post->post_parent)
    $children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&exclude='.$post->ID.'&echo=0'); else
    $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    if ($children) { ?>

  5. KAJ Family

    Eh… I am trying this on my wp and it did show when I logged in as admin. but I cannot see it as a guest…
    Do you guys know if there’s any setting I might have cause that?

    I’m using 2.6.2 with Atahualpa 2.01 theme

    This is the page I wanted to show -> http://blog.kajsolution.com/kajfamily/C/?page_id=247

    It should display a list of subpages that I have with thumbnails.

    Thank you ^^

  6. KAJ Family

    Sorry… didn’t notice that < > will be considered as html tag here ^^”


    <?php query_posts('static=true&posts_per_page=-1&child_of='.$id.'&order=ASC&orderby=title'); ?>
    <center><table><tr>
    <?php if(have_posts()) : $count = 0; while (have_posts()) : the_post();
    if ($count > 3) : $count = 0; ?></tr>
    <tr><td align=center colspan=8>
    <img src="<?php echo get_post_meta($post->post_parent, 'HR', true); ?>" />
    </td></tr><tr>
    <?php endif; ?>
    <td><a href="<?php the_permalink();?>"><?php the_title();?><br />
    <img src="<?php echo get_post_meta($post->ID, 'thumbnail', true); ?>" /></a></td>

    <?php $count++; endwhile; endif; ?>
    </tr></table></center>

  7. Problem solved!!! My problem is that I need to include “post_status=publish” when calling query_posts()
    so first line should look like this
    <?php query_posts('post_status=publish&static=true&posts_per_page=-1&child_of='.$id.'&order=ASC&orderby=title'); ?>

  8. He Westi, nice job for this wp sql tweak.

    Did you have any luck finding out how to make the “post_per_page=” or “showpost=” work?
    I can’t seem to figure this one out. I am running it on a 2.6.5 and 2.7 wp sites.

    Cheers

  9. He Westi,

    I know its not a query but the function: get_pages uses the child_of argument.

    maybe this is why they didn’t get to create the child_of for pages in the query. Anywyas I fiddled around with this and didn’t come to any conclusion but wanted to give you an insight from my search.
    cheers

Comments are closed.