Wordpress Wp_query - How to show parent post only?

Asked

Viewed 103 times

1

Good evening everyone, I have a CPT where each post has its child, when listing this with Wp_query, on the page is shown all posts (father and son), but I want to show only the posts 'father', how can I do this?

Thank you very much!

follows the code used:

 <!-- primeiro Loop -->
      <?php
       $args = array(
           'post_type' => 'novellist'                
       );
       $novels = new WP_Query( $args );

       if( $novels->have_posts() ):
          while( $novels->have_posts() ): $novels->the_post();
       ?>
       <!-- Post -->
       <div class="col-12 post1">
           <?php get_template_part('template_parts/content', 'archive-novel'); ?>
       </div>
       <!-- End Post -->
       <?php
            endwhile;
            wp_reset_postdata();
            endif;
       ?>
  • 1

    In the title you say post dad, in the body of the question you say * category father and daughter*. Who has children is the CPT or the category? They’re very different things.

  • I apologize for the mistake, I have a CPT called 'Novels List" and I have a taxomany called 'Novels' attached to the CPT. No CPT tenho o post pai (receiving the parent category) e o post filho( que recebe a categoria filho). I edited the post including

1 answer

1


For this, you can use the parameter post_parent. When a post has "0" as Parent, it means it has no father, so it is not post son.

 $args = array(
   'post_type' => 'novellist',
   'post_parent' => 0    
 );

(source)

  • I think I did something wrong, all the posts are gone when I use this argument

  • 1

    @Gustavosiqueira edited the answer after you clarified the ambiguity.

  • I apologize for my mistake, I ended up confusing the taxomanias with the posts, thank you very much for the help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.