Question

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!

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top