Loop through posts with and without children

MPortman :

I have this post type that I want to loop through. I have to create two different sections. The top works where it loops through all. But I want to exclude the parent that's holding the children (#6474) and everything in that parent to loop in a different row.

What I have so far. This works in regards of posting all posts. But at the moment includes all parent and children minus #6474. Just want this to show ones that are only parents.

Trying to figure out how to approach creating another row that will only show the children in the post type.


$customersPage_args = array (
    'post_type'     => array( $global_cat ),
    'post_status'   => array( 'publish' ),
    'posts_per_page'  => -1,
    'order'         => 'ASC',
    'orderby'       => 'publish_date',
    'post__not_in' => array(6474) //excluding the ID holding the children
);

$global_cat_query = new WP_Query( $customers_sort ); ?>

<h3 class="h2 display <?php echo $block[className]; ?>"><?php echo $block_heading; ?></h3>
<div class="card-row">
    <div class="card u-pb-0">
        <div class="row">
        <?php // The Loop
        if ( $global_cat_query->have_posts() ) :
            while ( $global_cat_query->have_posts() ) : $global_cat_query->the_post();  ?>
                <div class="col-md-3 col-sm-4 col-6">

                    <a href="<?php echo get_permalink(); ?>">
                        <div class="card card u-mt-0 u-mb-4 align-items-center">
                            <img src="<?php echo get_the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" />
                        </div>
                    </a>
                </div>

            <?php endwhile;
        endif;
        // Restore original Post Data
        wp_reset_postdata(); ?>
        </div>
    </div>
</div>


<?php endif; ?>

Xhynk :

If you only want top level items, you can make use of the post_parent parameter. If you set it to 0, it will only find "parent" (aka "Top-Level" posts):

$customersPage_args = array (
    'post_type'      => array( $global_cat ),
    'post_status'    => array( 'publish' ),
    'posts_per_page' => -1,
    'order'          => 'ASC',
    'orderby'        => 'publish_date',
    'post__not_in'   => array(6474), //excluding the ID holding the children
    'post_parent'    => 0, // Only get "Top Level" posts
);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=198204&siteId=1