Looping through child posts of a parent post

Using ACF, I created a custom post type (State Parks) and set it to be hierarchical.

I then created a single page template that is tied to the State Park post type with a conditional statement. The name of the single page template in Cornerstone is State Park Overview.

I created 3 additional posts (fees, dh hiking trail 1, dh hiking trail 2) with this custom post type and selected the post “Dead Horse Point” as the parent post in the child posts.

Single Page Template - The Looper Provider: Whether I use the query builder or query string in Cornerstone, I am unable to pull the 3 child posts of the parent post (Dead Horse Point).

I found this, but I don’t know where to place this code. To target children of the current post/page you are viewing, you can use a Dynamic Content string like {{dc:post:id}} as the value for the post_parent parameter.

Can you please tell me how to pull in the child posts of a current/parent post on my page template? Thank you.

Hello @dtsui,

Thanks for posting in!

The given WP access does not work for us. In the meantime, you can use this argument as your basis for a Looper Provider Query String:

$args = array(
    'post_type'      => 'state-park',
    'post_parent'    => 123,
    'posts_per_page' => -1,
    'orderby'        => 'menu_order',
    'order'          => 'ASC'
);

Running http_build_query($args) gives us post_type=state-park&post_parent=123&posts_per_page=-1&orderby=menu_order&order=ASC . You can replace 123 with the {{dc:post:id}} dynamic content code.

Kindly let us know how it goes.

Can you please try logging in again? The password has been updated. My apologies.

  1. In the meantime, can you please tell me where I would add the $args code that you provided? Functions.php?

  2. When you say run http_build_query($args) where would I run that?

  3. In addition, when you say replace “123” with {{dc:post:id}}, do you mean:
    post_type=state-park&post_parent={{dc:post:id}}&posts_per_page=-1&orderby=menu_order&order=ASC

Is the above line of code the query string that is added to Cornerstone? If so, can you please tell me where I would post the line above in Cornerstone?

Is there a way to loop through children posts of a parent post without adding code to the php files?

Thank you for your help.

Hello @dtsui,

1.) Yes, running http_build_query($args) means adding this in the child theme’s functions.php file. This is only done to get the query string post_type=state-park&post_parent=123&posts_per_page=-1&orderby=menu_order&order=ASC, which then we can use as the Looper Provider Query String.

3.) Yes, that is correct.

That line of code will be added temporarily. It is only for the determination of the Looper Provider Query String. Once you have the query string, which you already did, you can remove the code in the child theme and you are good to go. At the moment, you do not have to do anything because the sub pages where already displayed correctly.

I made some changes to your layout. I relocated the Looper Provider Query String in the Column element so that a condition can be added to the Column. When there are NO sub pages, that whole Column will not display.

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.