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 Responses to “Query Child Of $Page”

  1. […] you really need this plugins you can download it on this page. This plugins has been tested on WordPress 2.7 and work without any worries Share and […]

  2. Foroct Fralion says:

    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

  3. atsil says:

    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! :)

  4. […] popular request I have updated my Query Child Of plugin to support limiting the number of child pages returned (and to support the ability for this […]

  5. […] follow the white rabbit ยป Query Child Of $Page (tags: webdev wordpress plugins loop) […]

  6. Simon says:

    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!

  7. Derek Herman says:

    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.

  8. Derek Herman says:

    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!

  9. westi says:

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



d
go to dashboard
l
go to login
h
show/hide help
e
edit post/page
r
comment on post/page
m
go to moderate comments
esc
cancel
%d bloggers like this: