Support

Account

Home Forums Front-end Issues Child pages of parent with post object

Solved

Child pages of parent with post object

  • This might be a long shot, but is it possible to get a list of child pages under the parent where the parent is selected from a post object…?

    I tried;

    <?php
    	$post_object = get_field('second_subpage');
    
    	if( $post_object ): 
    
    	// override $post
    	$post = $post_object;
    	setup_postdata( $post ); 
    ?>
        <!-- here -->
    	<?php
    		$args = array(
    		    'post_parent'    => $post_object,
    		    'post_type'      => 'page',
    		    'posts_per_page' => -1,
    
    		    'order'          => 'ASC',
    		    'orderby'        => 'menu_order'
    	 	);
    
    		$parent = new WP_Query( $args );
    
    		if ( $parent->have_posts() ) : 
    		while ( $parent->have_posts() ) : $parent->the_post();
    
    	?>
    		<div class="page_link page_link--small count_five" style="background-image:url('<?php the_post_thumbnail_url(); ?>');">
    			<div class="page_link__title">
    				<h2><?php the_title(); ?></h2>
    				<?php the_field('preview'); ?>
    			</div>
    		</div>
        <?php endwhile; ?>
    	<?php endif; wp_reset_postdata(); ?>
        <!-- end -->
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

    But it just pulled through all the pages, not the child pages of parent.

  • Hi ForbiddenChunk! You should use the post ID instead of the post object in the post_parent argument. Take a look at this, may helps you in the future https://gist.github.com/luetkemj/2023628

    
    <?php
    $post_object = get_field('second_subpage');
    
    if( $post_object ): 
    
    // get the post id from the post object
    $post_ID = $post_object->ID;
    
    // override $post
    $post = $post_object;
    setup_postdata( $post ); 
    ?>
        <!-- here -->
    	<?php
    	$args = array(
    	    'post_parent'    => $post_ID,
    	    'post_type'      => 'page',
    	    'posts_per_page' => -1,
    
    	    'order'          => 'ASC',
    	    'orderby'        => 'menu_order'
     	);
    
    	$parent = new WP_Query( $args );
    
    	if ( $parent->have_posts() ) : 
    	while ( $parent->have_posts() ) : $parent->the_post();
    	?>
    
    	<div class="page_link page_link--small count_five" style="background-image:url('<?php the_post_thumbnail_url(); ?>');">
    		<div class="page_link__title">
    			<h2><?php the_title(); ?></h2>
    			<?php the_field('preview'); ?>
    		</div>
    	</div>
    
    <?php endwhile; ?>
    <?php endif; wp_reset_postdata(); ?>
    
        <!-- end -->
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    
  • That’s perfect thanks! I had the feeling I wasn’t far off.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.