I have this code that shows all the posts in my post type (parents and children). the php is:
<div class="owl-carousel owl-theme">
<?php
$c=0;
$q2=new WP_Query(array('post_type'=> array( 'post', 'book' ),'post_status'=> 'publish','orderby'=>'modified','order'=>'desc'));
while($q2->have_posts()) {
$c++;
$q2->the_post();
?>
<li class="item">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<div class="metaitem">
<ul>
<li><?php the_title(); ?></li>
<li><?php the_excerpt(); ?></li>
</ul>
</div>
</a>
</li>
<?php
}
wp_reset_postdata();
?>
</div>
now what array i can use to show only the parents and don’t show the children? i have used 'parent' => 0 but it didn’t work!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
According to the documentation, you would have to use post_parent => 0
So your code would look like this:
<div class="owl-carousel owl-theme">
<?php
$c = 0;
$q2 = new WP_Query( array(
'post_type' => array( 'post', 'book' ),
'post_status' => 'publish',
'post_parent' => 0, //add this here
'orderby' => 'modified',
'order' => 'desc'
) );
while ( $q2->have_posts() ) : $q2->the_post();
$c ++;
?>
<li class="item">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<div class="metaitem">
<ul>
<li><?php the_title(); ?></li>
<li><?php the_excerpt(); ?></li>
</ul>
</div>
</a>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</div>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0