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. Pingback: WordPress Plugin Releases for 02/25 | Wordpress Blog NL

  2. Pingback: Query for Children of the current page plugins / WordPress Themes, Plugins & Development

  3. Foroct Fralion

    I’m having the same problem that Matthew had, I am showing my subpage query but after the query ends the first sub page title and content show underneath.

    my query is query_posts(‘post_status=publish&static=true&posts_per_page=-1&child_of=’.$id.’&orderby=menu_order’);

    thanks

  4. Using westi’s stuff of his first answer together with the PJW-excerpt-plugin I’m absolutely happy with the result. Which will be completed and online soon on my website (check “Support Humanity”). Thx a lot! 🙂

  5. Pingback: follow the white rabbit » Updated child queries, now with limits!

  6. Pingback: JeremiahTolbert.com » Blog Archive » links for 2009-03-25

  7. This plugin is great, so powerful. Just one quick question if you’ve got the time though.

    I have set a page to be used as the index and want to list the child pages of that. The code I have used on the homepage is:

    query_posts(‘static=true&posts_per_page=-1&child_of=’.$id.’&order=ASC’);

    The problem is that it’s not just listing the pages I have set as children to the homepage but other root pages and even the homepage itself.

    Is there a way I can make it only query those pages explicitly set as children to the homepage. If not then what code can I use to exclude particular page id’s?

    Thanks again for the plugin!

  8. I am trying to figure out how to paginate the subpages of my portfolio section. So if I am using the code above to list the most recent 10 pages on the actual portfolio page and want to paginate the remaining pages how would this be accomplished?

    I tried:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('post_status=publish&static=true&child_of='.$post->ID.'&child_limit=10&paged=' . $paged);
    $wp_query->is_archive = true; $wp_query->is_home = false;

    But it does nothing. Any suggestions would be helpful. Thanks in advance Peter.

  9. Never mind, I figured out how to paginate the results. It’s a bit crude but it works. Here is the code I used:

    Add this to your functions.php:

    // Get the id of a page by its name
    function get_page_id($page_name){
    global $wpdb;
    $page_name = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$page_name."'");
    return $page_name;
    }

    And then use this query in your template:

    $limit = 9; // Change this to the number of pages to show
    $parent_id = get_page_id('portfolio');
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if ($paged > 1) {
    $child_offset = ($paged - 1) * $limit;
    } else {
    $child_offset = 0;
    }
    $numposts = $wpdb->get_var("SELECT COUNT(*)
    FROM wp_posts
    WHERE post_parent = {$parent_id}
    AND post_status = 'publish'");
    query_posts("post_status=publish&static=true&posts_per_page={$limit}&child_of={$parent_id}&child_limit={$limit}&child_offset={$child_offset}&paged={$paged}");

    Then make sure you have more than one page and show the pagination (I use wp-pagenavi):

    if($numposts > $limit) { // Show Pagination
    wp_pagenavi();
    }

    I hope this helps anyone interested in paginating their subpages, enjoy!

  10. @Simon: Strange I’m not sure why thats not working. Can you check it works for you if you use it on another page?

    @Derek: Great to see you got the pagination work. Support for it was something I added on a whim when updating the plugin with the limit code we worked on. Great to see an example of how to use it.

Comments are closed.