WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to create a page which display a few of sub pages (child pages), however if the page has not child I would like to display a message.

I cannot get a query that allow me to do something like this:

 if (Page has childs) {
    do this
 else {
    display this

Any help?

Thanks in advanced

share|improve this question
Just a note, you may want to start accepting answers on some of your questions (even ones from the past) if you have been given sufficient answers. This is to ensure completeness around the site and a low accept rate means people won't be too eager to respond. :) – Jared Jan 20 '12 at 22:42
I accept em all the time, I haven't had the time to check this specific since I didn't spect to get the answer so fast and is friday night so basically will be a few hours until I get in my computer again, also I saw u gave me an answer, I'd appreciated your time, and will be happy to give you the points later, Regards – andresmijares25 Jan 20 '12 at 23:16
Oh that's alright, was just a friendly reminder. Just trying to do the best I can to contribute here! :) – Jared Jan 20 '12 at 23:30
up vote 2 down vote accepted
$args = array(
    'post_status' => 'publish',
    'post_parent' => $post_id
$children = get_posts( $args );

if( count( $children ) > 0 ) {
    //has children
} else {
    //does not have children

Docs: get_posts()

This should also allow you to have access to the data of the children, should you need it. If you need any additional parameters in $args (a post type, for example), make sure to include those.

share|improve this answer

What about using something like this (provided you have a way to get the ID of the page you are checking for child pages):

$children = get_pages( array( 'child_of' => $post->ID ) );

However if you want to get just direct descendants, see m0r7if3r's, or this would also work too:

$children = get_pages( array( 'child_of' => $post->ID, 'parent' => 0 ) );

And then check that using count($children) with an if statement like so:

if( count( $children ) > 0 ) {
    // has children (and grandchildren if not using the 'parent' param)
} else {
    // does not have children
share|improve this answer
Could use a few more args, but I like the way you think ;) – mor7ifer Jan 20 '12 at 22:30
Yeah definitely lol, this will also grab the grandchildren of the page if any, 'parent' => 0 would be more suitable if he's just looking for direct descendants along with the child_of parameter. – Jared Jan 20 '12 at 22:35

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.