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. @Simon are you using a custom homepage? You might want to grab the id of the actual page and add it in explicitly by using the function I have above. I had issue when using the post id like that for some reason too. It would return all the pages and not just subpages of the page I wanted. Id’s get messed up on the homepage I think due to it becoming is_home() instead of the page you think it is. Do this:

    Add to 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;
    }

    Change your query to:

    query_posts(’post_status=publish&static=true&posts_per_page=-1&child_of=’.get_page_id('pagename').’&order=ASC’);

    Good luck.

  2. Karl

    Hi there,

    Not sure if this is possible with the plugin, but I’m trying to get the grandchildren of a page only, not the children. I have a page called “our courses”, then subpages which list the different “sections” and a bit of information, then under each section I’ve got the relevant “courses”. On my “our courses” page, I’d like to fetch all the courses in a loop, so I can output the title and some custom fields with information.

    Is there a way to use depth= or similar, so that on “our courses”, I can set child_of=35 (my ID) and then only list grandchildren of this page?

    Thanks for any hints!

    1. @Karl: Getting the grandchildren of a page is going to be an interesting task!

      Because of the way the data is structured it isn’t possible to extract this info from the datastore in a single query so you are probably best off using nested WordPress queries to do this.

      Something like this may work:

      
      $outer_query = new WP_Query('post_status=publish&static=true&posts_per_page=-1&child_of=$grandparent_page_id&order=ASC’);
      
      while ($outer_query->have_posts()) : $outer_query->the_post();
      {
          $inner_query = new WP_Query('post_status=publish&static=true&posts_per_page=-1&child_of={$post->ID}&order=ASC’);
          while ($inner_query->have_posts()) : $inner_query->the_post();
          {
              //Template Tags to output content here should be working on grandchild posts.
          }
      }
      

      Note that once you start the while loop for the inner_query you lose access to the current entry in the outer query via the post globals so you can only use template tags on pages in the outer query before the while loop for the inner query.

      Let us know how you get on with this!

  3. Karl

    @Peter,

    Many thanks for your input on this! I think it would be a handy loop to make. Ideally, I’d like to group the results by child, something like:

    CHILD 1
    -Grandchild 1.1
    -Grandchild 1.2

    CHILD 2
    -Grandchild 2.1
    -Grandchild 2.2

    Reading your comment about two loops and passing variables in the outer one first, perhaps this is possible if it cycles through a child, its children and then on to the next child?

    I tried your code and added endwhile’s where I thought they should be. I also added a title-tag. However, what I’m getting on the page is a long list of all pages in the system (39 of them), looped through 39 times! Obviously your snippet above was to point me in the right direction and not something that would work out-of-the-box, but it would be great if you had some more details about what else could be needed to get the code to work! Would really appreciate it!! This is the code I’ve run now:

    $outer_query = new WP_Query(‘post_status=publish&static=true&posts_per_page=-1&child_of=$grandparent_page_id&order=ASC’);
    while ($outer_query->have_posts()) : $outer_query->the_post();
    {
    $inner_query = new WP_Query(‘post_status=publish&static=true&posts_per_page=-1&child_of={$post->ID}&order=ASC’);
    while ($inner_query->have_posts()) : $inner_query->the_post();
    {
    echo ” . the_title() . ”;
    }
    endwhile;
    }
    endwhile;

  4. @Karl I’m fairly certain you didn’t need to add the endwhile; to each loop because the syntax was written with while { instead of while : so the } closed the while loop.

  5. Karl

    @Derek,

    Thanks for the reply. I see, didn’t know that about { and while : !

    I had tried without the endwhile first though; if I take both of them out as in the original example, I get an error saying that there’s an unexpected curly bracket for the last of the two closing ones. So perhaps it’s missing something else?

    Parse error: syntax error, unexpected ‘}’ in [..link to the file..]

  6. Derek Herman

    Hmm, weird I guess the syntax is odd and I’m wrong. The : style of writing php always confuses me.

  7. Karl

    Hallo!

    Still haven’t managed to crack this nut, have tried a bunch of different variations to get an output like the one above. Perhaps getting just the grandchildren data isn’t so common, or required, as I can’t seem to find much info.. so any further input on this would be great! Would be great to have a function where one can not only run a query for child_of, but also grandchild_of!!

    1. @Karl: I’m pretty sure this will be possible using the plugin as it stands. I just need to firm up the template code required to do it. Been off relaxing over the past week so not had time to look at it. Please bear with me and I hope to have a solution for you soon!

  8. Karl

    @Westi,

    Would be great if you managed to come up with that loop!! I’m eager ears when you’ve got a moment to try it out.

    Thanks, karl

  9. haim

    hi been looking for something like that for a long time!
    but i cant get it to work properly…
    it limit the and get the paging but for example when i click on next page the content doesn’t change
    someone have any idea what i am doing wrong?
    here is my code:

  10. Here is how I paginate:

    // Change the limit to fit your needs
    $limit = 9;
    $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'”);

    // To query the pages
    query_posts(“post_status=publish&static=true&posts_per_page={$limit}&child_of={$parent_id}&child_limit={$limit}&child_offset={$child_offset}&paged={$paged}”);

    // Count the subpages and show the pagenavi
    if($numposts > $limit) {
    wp_pagenavi();
    }

Comments are closed.