0

In the Parent Post template (say single-country.php for 'country' CPT), I want to show the permalinks for its Children Posts. In this case, let's say all children CPT's are on the same level, so 'state', 'province', 'prefecture' and 'department' CPT's (which are children of 'country') would be all "brothers", so no grand-children.

Now, I know I can do:

echo '<div class="div-children">';

    $args = array(
        'post_type' => array(
            'state',
            'province'
        ),
        'posts_per_page' => -1,
        'post_status'    => 'publish',
        'post_parent'    => $post
    );

    $child = get_posts( $args );

    if ( $child ) {
        echo '<p class="p-children"><a href="' . get_permalink( $child[0]->ID ) . '" alt="States">States</a></p>';
        echo '<p class="p-children"><a href="' . get_permalink( $child[1]->ID ) . '" alt="Provinces">Provinces</a></p>';
    }

echo '</div>';

...and I get the Permalinks for the first posts for the 'state' and 'province' Children CPT's. That's fine, but that adds 3 more queries to the Database.

Then, I know I also can do:

echo '<div class="div-children">';

    $parent_id = wp_get_post_parent_id( $post->ID );
    $my_wp_query = new WP_Query();
    $all_wp_pages = $my_wp_query->query( array(
        'post_type' => array(
            'state',
            'province'
    ),
        'posts_per_page'=> -1,
        'post_status'   => 'publish',
        'post_parent'   => $post
    ) );

    $child = get_page_children( $parent_id, $args );
    if( $child ) {
        echo '<p class="p-children"><a href="' . get_permalink( $child[0]->ID ) . '" alt="State">State</a></p>';
        echo '<p class="p-children"><a href="' . get_permalink( $child[1]->ID ) . '" alt="Province">Province</a></p>';
    }

    wp_reset_postdata();

echo '</div>';

...but now we're adding 4 queries to the database.

So, there would be a third option:

echo '<div class="div-children">';

    $parent_id = wp_get_post_parent_id( $post->ID );

    $args = array(
        'post_type' => array(
            'state',
            'province'
        ),
        'posts_per_page' => -1,
        'post_status'    =>'publish',
        'post_parent'    => $post
    );

    $child = get_page_children( $parent_id, $args );

    if( $child ) {
        echo '<p class="p-children"><a href="' . get_permalink( $child[0]->ID ) . '" alt="State">State</a></p>';
        echo '<p class="p-children"><a href="' . get_permalink( $child[1]->ID ) . '" alt="Province">Province</a></p>';
    }

echo '</div>';

This third option uses get_page_children (as does the second one), but doesn't add a single database query. The problem is, of course, it doesn't work the way I've written it. But it sort of "works": The Permalinks shown are for the Parent Post, but I need the Permalinks for the Children Posts.

I tried several variations and searched in Google and SO, S.Exchange, etc, but nothing. Ideally, I would use something like get_permalink ( $child[x]->ID )in the third option (once I can make the child-posts appear), since I know that, for my particular case, I will have always only one post of each children CPT.

Also, I don't understand exactly why this only seems to work with $parent_id = wp_get_post_parent_id( $post->ID );I know it won't work with $parent_id = get_the_id();because it needs to go inside the query, but I don't get why something like $parent_id = $post->ID; doesn't work either.

It would be great if someone could help me with the third option so my code is correct and efficient.

Thanks in advance.

  • Wouldn't it be easier to just make the "child post types" taxonomies of a single post type instead? – Howdy_McGee Jan 19 '17 at 17:56
  • No, it wouldn't. It works for some cases, but not in this one. I need child posts to be posts. There are some custom taxonomies already made, which work as what they are, taxonomies. – Karls Jan 19 '17 at 18:08
  • Are all the above post types really top level or is it just simplified for this example? Will posts change often? – Howdy_McGee Jan 19 '17 at 18:37
  • 'country' is top level, 'state' and 'province' are children of 'country'. Both are set as hierarchical posts with 'country' as parent. Note that the two first snippets work but they add 3 (the first) and 4 (the second) more queries to the database for each children cpt. The third one "works", but showing the current parent 'country' cpt permalink. Nope, they won't be changing. – Karls Jan 19 '17 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.