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

Read 21 Comments

21 Comments »

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=true as well as the child_of argument 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!!! :D 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 :D

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 §

 

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

21 queries. 1.043 seconds. Powered by WordPress. This blog is protected by Spam Karma 2: 208537 Spams eaten and counting...