Stepholt News

Get Child Pages of Parent in WordPress with Advanced Custom Fields

News Categories: Wordpress

This article was published on: 03/11/20 4:49 PM

How to get Child Pages of Parent in WordPress with Advanced Custom Fields

In a recent white label website development, the client of a housing development company required a bespoke house listing and we love using ACF Pro for all our projects as we think it’s user-friendly for the customer in the back end.

If you use ACF then getting them to show on the front end can be tricky, especially if you put them in a group or repeater field, below is the code we used to get the child page house type listings for the housing development which is a parent page.

Get the ‘house_details’ which is the ACF group within the WordPress admin panel

$housedet = get_field(‘house_details’, $page->ID);?>

If you have any questions please comment below or drop us an email for any help of advice.

$ancestor_id = $post->$page_id = get_queried_object_id();
$descendants = get_pages(array(‘child_of’ => $ancestor_id));
$incl = “”;
foreach ($descendants as $page) {
if (($page->post_parent == $ancestor_id) ||
($page->post_parent == $post->post_parent) ||
($page->post_parent == $post->ID)){
$incl .= $page->ID . “,”;}}

php $mypages = get_pages( array( ‘child_of’ => $ancestor_id, ‘sort_column’ => ‘post_date’, ‘sort_order’ => ‘desc’ ) ); foreach( $mypages as $page ) {$content = $page->post_content; if ( ! $content ) // Check for empty page continue; $content = apply_filters( ‘the_content’, $content );

$housedet = get_field(‘house_details’, $page->ID);

Use the following to load in each ACF field:

php echo $housedet[‘house_type_description’];

 

 

We have removed starting and closing tags for readability but you can use the code and amend as needed.

Website Design
Website Development
Website Marketing