Exclude parent posts and display only child posts in archive Exclude parent posts and display only child posts in archive wordpress wordpress

Exclude parent posts and display only child posts in archive


I know it's an old question but hoping I can help someone that finds their way here looking for the same thing I was.

You can show ONLY child posts by excluding any posts with post_parent = 0 using the 'post_parent__not_in' argument:

$args = array(    'orderby' => 'date',    'order'   => 'DESC',    'post_type' => 'city-guide',    'posts_per_page' => 36,    'paged' => $paged,    'post_parent__not_in' => array(0));

This avoids the need to loop thru each parent post to get each child.


I see you are trying to push the IDs into an array but why not just use the IDs while you are looping through them while getting the children within the loop at the same time? The example below is how I would tackle this.

<?php$args = array(    'orderby' => 'date',    'order'   => 'DESC',    'post_type' => 'city-guide',    'posts_per_page' => 36,    'paged' => $paged    );$query = new WP_Query( $args );$i=1; while( $query->have_posts() ): $query->the_post();$parentID = get_the_ID();$childrenArgs = array(            'post_type' => 'page',            'post_parent' => $parentID ,            );        $children = get_children($childrenArgs);        foreach ($children as $child){            echo '<h1>' . $child -> post_title . '</h1>';            $content = $child -> post_content;            $content = apply_filters('the_content', $content);            $content = str_replace(']]>', ']]>', $content);            echo $content;        }endwhile;?>


I think you need to look into the action pre_get_posts. Something like this in your functions.php would do the trick.

function namespace_custom_query_vars( $query ) {  if ( !is_admin() && $query->is_main_query()) {    if ( $query->query["post_type"] == 'custom_post_type' ) {      $query->set( 'post_parent__not_in', 0 );    }  }  return $query;}add_action( 'pre_get_posts', 'namespace_custom_query_vars' );

There's a decent post about this here. Though note that the code on this page does not compile for small syntax errors.