Reputation: 181
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; ?>
Upvotes: 1
Views: 106
Reputation: 13880
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
);
Upvotes: 1
Reputation: 169
You can use this function to detect if a post has a parent: https://developer.wordpress.org/reference/functions/wp_get_post_parent_id/
Upvotes: 0