How to display only top level posts in loop via WP_Query?

The question:

How do I set my custom loop to only show top-level posts? I have a hierarchical custom post type and the archive page shows both the parent and child posts.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

This solution is based on some code by Justin Tadlock. pre_get_posts is called before WordPress gets the posts of the main loop. Basically, you test to see if the page is the archive of the post type and make sure post_parent hasn’t been set. Then you set post_parent to 0, which is the default parent of top level posts. Easy as pie.

 <?php
    //pre_get_posts filter is called before WordPress gets posts
    add_filter( 'pre_get_posts', 'my_get_posts' );
    
    function my_get_posts( $query ) {
        //If the user is viewing the frontend, the page is an archive and post_parent is not set and post_type is the post type in question
        if ( ! is_admin() && is_archive() && false == $query->query_vars['post_parent'] &&  $query->query_vars['post_type'] === 'my_post_type')
            //set post_parent to 0, which is the default post_parent for top level posts
            $query->set( 'post_parent', 0 );
        return $query;
    }
    ?>

Method 2

You can just add post_parent=0 to your query

Method 3

Elaborating from @Ryan’s post, the key is setting post_parent=0 and post_type='page'.

You can always view the SQL request of the WP_Query Object to see what arguments you need to add to get your desired results.

This code works for me:

<?php
$args=array('post_parent' => 0, // required
                'post_type' => 'page', // required
                'orderby' => 'menu_order', // to display according to hierarchy
                'order' => 'ASC', // to display according to hierarchy
                'posts_per_page' => -1, // to display all because default is 10
    );

    $query = new WP_Query( $args ); 

    /*  Uncomment to see the resulting SQL to debug
    echo $query->request; die();
    //*/

    if ( $query->have_posts() ) {
        while($query->have_posts()) {
            $query->the_post();
            $post_id=get_the_ID();
            $post=get_post($post_id,'ARRAY_A');                
            echo $post['ID'].': '.$post['post_title'].'<br>';
        }            
    }


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

Leave a Comment