March 30th, 2008 › Query Child Of $Page
Current Release: 0.02 (download) - Released on January 8th 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 argument to the list of arguments supported by query_posts(). The new 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.
Requirements
This plugin has been tested with WordPress v2.3.2 and should work with all WordPress versions that support pages (v2.0 and later).
Download
The latest version of the plugin may be downloaded here: pjw_query_child_of.0.02.zip

Hi Peter,
This plugin seems to be exactly what I want however I can’t get it to function properly.
I want to call all the subpages of my portfolio page, loop through them and display some custom field data.
for this I am using the following code
<?php query_posts('child_of=44'); while (have_posts()) : the_post(); ?> <?php the_title() ?> <img src="?php echo get_post_meta($post->ID, "screenshot", true); ?>" /> <?php echo get_post_meta($post->ID, "excerpt", true); ?> <a href="?php echo get_post_meta($post->ID, "url", true); ?>">Visit the site <a href="?php the_permalink() ?>">Read the case study » <?php endwhile; ?>The problem is that this is simply displaying literally everything in my blog, including posts, pages, uploaded files, basically everything with an ID.
Could this be that this plugin doesn’t work with Wordpress 2.2.2 (the version I am using).
I really need this functionality and if it comes with it I will pay.
Thanks, Tom
Comment by Tom Willmot — 6/9/2007 @ 4:45 pm §
Tom: It definetely works with 2.2.2 (as that is what I am currently running and it powers my WordPress page).
This is what I have in my template file for that page:
<?php query_posts('static=true&posts_per_page=-1&child_of='.$id.'&order=ASC'); ?> <?php if(have_posts()) : while (have_posts()) : the_post(); ?> <dt><a href="<?php the_permalink();?>"><?php the_title();?>:</a></dt> <dd style=""><em><?php the_excerpt(); ?></em></dd> <?php endwhile; endif; ?>Try updating your query to add those few extra arguments and see if that helps.
Otherwise let me know and I’ll look into it some more.
Comment by westi — 6/9/2007 @ 5:29 pm §
Hi Peter,
Thanks for quick reply, have tried using your code and all its variations, still no luck, if I simply copy the code that you have posted then the page shows a list of everything on the site,
I have no extra plugins running.
Could you perhaps send me all the code from the template so that I can compare it all.
Tom
Comment by Tom Willmot — 6/9/2007 @ 7:05 pm §
Tom: I’ve sent you an email with the whole page template let me know if you have any more problems.
Comment by westi — 7/9/2007 @ 4:57 pm §
Is there a way I can have it show children of “current page”? I want to use this in a theme template that will be used for multiple pages (essentially I want to create a “section index” template).
This combined with you “Page Excerpt” plugin is a powerful combination. Outstanding!
Comment by Stephen Rider — 1/10/2007 @ 3:46 pm §
Stephen: The code I use in my template for my WordPress plugins page does just that - I have pasted the basic template code I use in a comment above - I guess I should update the page itself with some example code!
Comment by westi — 1/10/2007 @ 4:31 pm §
Hi there!
I have the same problem as Tom. The plugin doesn’t seem to work. I take two guesses. I’m querying for child pages of a child page… maybe it messes the query up?? Also it works differently when you have two queries on the same template.
Please, let me know if you found the answer, since I really find your plugin a great idea!
Comment by Alicia — 5/1/2008 @ 10:45 pm §
Alicia: What parameters are you including in the call to query_posts()? You need to have the
static=trueas well as thechild_ofargument as this tells WordPress that you are asking for Pages.Comment by westi — 6/1/2008 @ 9:46 am §
Hi!
This is my query:
But it shows everything that’s inside my blog (including non-static pages)
My page’s structure is this:
– Level 1
– Level 2
———- Sublevel 1
———- Sublevel 2 <– This is the page I’m building the template for
——————– Sub-sublevel 3 <– These two sub-subpages is what I want to show on the template
——————–Sub-sublevel 4
Thanks for your time!
Comment by Alicia — 7/1/2008 @ 11:02 am §
Peter –
I’m still not able to get this to work. I’ve tried exactly the example code you’ve shown us, plus I’ve tried hard-coding in a page ID. Same result. I’ve pared it down to
query_posts('static=true&child_of=7');and it still returns everything in my blog.Has anyone else gotten this to work? There are commenters saying this exact problem, and I don’t see anyone coming back to say “It worked!”
I’m on wp-hackers. I would love to get this to work — if you’d like me to work with you to get this working, I’m game.
Personally I think this functionality (and page excerpts — that plugin works great thanks!) should be in core.
Comment by Stephen R — 8/1/2008 @ 5:27 pm §
BTW — somethings wrong with your gravatars (???)
Comment by Stephen R — 8/1/2008 @ 5:29 pm §
There’s a bug in the code — that’s why nobody can get it to work. To fix it, replace this:
} else {
return $where;
}
with this:
}
return $where;
Comment by Stephen R — 8/1/2008 @ 8:42 pm §
Stephen: Firstly - Gravatars should now be fixed I was using a very old version of skippys gravatars plugin which had caching and other features from back in the days when gravatar couldn’t handle the load.
Secondly, that change will fix it - That is what the code I am running has in it! Somehow the code in the zip available for download is different from the code I am running!
Comment by westi — 8/1/2008 @ 8:46 pm §
[...] the plugin which will actually work for other people! If you have attempted in the past to use my query child of $page plugin and failed please head over to the page and download a working copy. Comment on this [...]
Pingback by Peter Westwood » Query Child Of $Page - update to make it work for anyone apart from me! — 8/1/2008 @ 9:04 pm §
Hi there!!!
Coool! It works now!!!
Geeez! Thanks a lot!
Comment by Alicia — 9/1/2008 @ 8:15 am §
[...] PJW Query Child Of [...]
Pingback by Nerdaphernalia » Automated Indexes — 9/1/2008 @ 4:21 pm §
Hey there, this pluging worked phenominally, until I upgraded to WP 2.5 *cry*
Is there any way to get it to work with the new Wordpress? Thanks in advance.
Comment by Lyanna — 30/3/2008 @ 10:24 am §
@Lyanna: The plugin works fine with WordPress 2.5 - I am using it on this site with WordPress 2.5 myself. Have you got version 0.02? There was a bug in version 0.01 which meant it didn’t actually work.
Comment by westi — 30/3/2008 @ 11:28 am §
@Westi
Sorry! Upon scrolling up I see I’ve ended up at the wrong plugin page. I came here via my wordpress plugin dashboard by clicking on the plugin name … I’m actually talking about the page excerpt plugin - apparently the wordpress interface links here instead of to the page excerpt plugin page
I’ll hop on over to the correct plugin page and make a new comment.
Comment by Lyanna — 30/3/2008 @ 12:50 pm §
The plugin works nicely, except that it is displaying also one child page that is not published.
Comment by Alice — 17/4/2008 @ 1:11 am §
@Alice: I suspect you need to be more specific in your call to query_posts() to ensure you only get published pages. You need to add ‘post_status=publish’ to the argument list.
Comment by westi — 17/4/2008 @ 6:51 pm §
This is just what i’ve been looking for. I will try it out when i get home from work tonight.
By the way, is there a way to also, apart from the excerpt and page title, display the page’s meta data. Something like this:
May 15th, 2008 - 12:00 pm
Page title (link to the page)
This is the page’s excerpt
Comment by Anders — 16/5/2008 @ 6:24 am §
hi!
nice plugin, great work! there is a way to sort pages by parameter: sort_column=menu_order ?
thanks!
Comment by naires — 27/5/2008 @ 6:44 pm §
I can’t seem to get this working. I’m trying “
query_posts('child_of=2');” and its pulling back 0 results. But if I use “get_pages('child_of=2');” I get 6 results (the expected amount). I know I could use “get_pages” but I really would like to use “The Loop” for the work I’m doing.Comment by Adam — 28/6/2008 @ 1:21 am §
@Adam: You need more arguments in your query to get it to work. Try something like:
query_posts('static=true&posts_per_page=-1&child_of=2');Comment by westi — 11/7/2008 @ 5:20 pm §
Hi Peter,
Great plugin. How do I limit the # of children being displayed? The conventional showposts=# is not working. Any help would be great. Thanks.
Comment by Adriel R — 15/7/2008 @ 12:38 am §
@Adriel: If my memory serves me correctly, you need to use post_per_page= to limit the number of posts returned.
Comment by westi — 16/7/2008 @ 8:06 am §
Thanks Peter!
Comment by Adriel R — 17/7/2008 @ 10:33 pm §
hello, would you mind helping me? I am not sure how to use this plugin. Do I need to put code within my template code? On my ‘hub’ Pages, I have a brief passage of text as an introduction. this introduction is my Page content and then I would like to list the child pages below.
thank you
Comment by James — 28/7/2008 @ 9:37 am §
hello, I’ve gathered I have to add the code above (or equivalent) to my template but as it is using query_posts() am I correct in thinking that I need to place this outside of the loop?
Comment by james — 28/7/2008 @ 5:53 pm §
@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.
Comment by westi — 28/7/2008 @ 8:34 pm §
ah ok thanks for replying. I will give it a go!
Comment by james — 28/7/2008 @ 11:15 pm §
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?
Comment by james — 29/7/2008 @ 10:43 am §
@james: yes thats fine. You can email me via my contact form when you have any questions.
Comment by westi — 29/7/2008 @ 4:49 pm §
@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!
Comment by Tom — 1/8/2008 @ 1:23 pm §
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.
Comment by Alice — 18/8/2008 @ 12:42 am §
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!
Comment by KandB — 27/8/2008 @ 7:28 am §
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’)
Comment by KandB — 29/8/2008 @ 1:17 am §
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
Comment by Paul — 26/9/2008 @ 9:26 am §
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.
Comment by westi — 27/9/2008 @ 10:21 am §
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
Comment by Ashish — 3/10/2008 @ 12:55 pm §
I believe that child_of=”.$post->ID. is different from child_of=’.$id.’
Did you try them both?
Comment by Alice — 3/10/2008 @ 6:40 pm §
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.
Comment by Ashish — 5/10/2008 @ 2:17 pm §
RSS feed for comments on this post. TrackBack URI
Leave a comment