0

I have a hierarchal custom post type called “courses”.

When you arrive on a course post there’s a check to see if the post has children. If the courses post has children it runs get_posts to display a list of those children.

Here’s where I’m struggling; part of the detail of a child list item is to display the number of children that child post has. So what I’ve tried to do is run a get_posts within a get_posts, but this is not working as expected.

To put things in context, and perhaps explain things better, here’s some of the hierarchy:

  • Adobe > Acrobat > Introductory Course
  • Adobe > FrameMaker > Advanced Course
  • Adobe > FrameMaker > Introductory Course

So when a visitor clicks on “Adobe” they get a list of the children, two items:

  • Acrobat; &
  • Framemaker.

Underneath these titles should be detail of how many sub-courses are available, so Acrobat should say; “1 course available” and Framemaker should say “2 courses available”.

This doesn't work. The sub get_posts returns nothing. My specific question is; how do I find out how many children a child has when inside a get_posts loop. Or should I try a different approach?

My current (simplified) code...

$postid = get_the_ID();
$kids = array(
    'post_type' => 'courses',
    'post_parent' => $postid
);
$children = get_posts( $kids );
if ( $children ) {
    echo '<ul>';
    foreach( $children as $child ) {
        $childid = $child->ID;
        $coursetitle = get_the_title( $childid );
        $grandkids = get_posts( array(
            'post_type' => 'courses',
            'post_parent' => $childid
        ) );
        $grandchildren = count($grandkids);
        echo '<li>';
        echo $coursetitle;
        echo $grandchildren . 'courses available';
        echo '</li>';
    }
}

Any help from you wise ones would be much appreciated.

7
  • You can use get_terms to retrieve an array of children of a specific term (in your case "Adobe"). Then take the length of that array.
    – cjbj
    Commented Jun 20, 2016 at 16:14
  • you don't set post_type, so it defaults to post.
    – Milo
    Commented Jun 20, 2016 at 16:33
  • Hey @cjbj thank you for the response. Can you help me understand "take the length of that array". How would I use, in my case, get_terms to work out how many children the post Framemaker has?
    – morgyface
    Commented Jun 20, 2016 at 16:37
  • Hey @Milo, sorry, over simplification on my part, post_type is in there. The count returns "1" regardless of the number of children.
    – morgyface
    Commented Jun 20, 2016 at 16:41
  • Are everything under Adobe posts, or are they terms Commented Jun 20, 2016 at 16:59

1 Answer 1

0

Your code should work, it might be that your posts per page option in back end is set to 1. It would be beneficial to explicitely set posts_per_page in your query args. You can also make the code faster if you only need to know the amount of grandchild posts, you can just return the post ids's.

Here is an example: (Copied and modified from your code)

$postid = get_the_ID();
$kids = array(
    'posts_per_page' => -1,
    'post_type'      => 'courses',
    'post_parent'    => $postid
);
$children = get_posts( $kids );
if ( $children ) {
    echo '<ul>';
    foreach( $children as $child ) {
        $childid     = $child->ID;
        $coursetitle = get_the_title( $childid );
        $grandkids   = get_posts( array(
            'posts_per_page' => -1,
            'fields'         => 'ids', // Only get post id's, much faster
            'post_type'      => 'courses',
            'post_parent'    => $childid
        ) );
        $grandchildren = count( $grandkids );
        echo '<li>';
        echo $coursetitle;
        echo $grandchildren . 'courses available';
        echo '</li>';
    }
}

If that does not work, you most probably have something intefering with your query, maybe a bad pre_get_posts action somewhere. To test this out, add remove_all_actions( 'pre_get_posts' ); before your query.

You can also have a look at get_pages() to run your queries which will not be altered by the normal WP_Query filters and actions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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