How to query for posts (in hierarchical custom post type) that have children?

You can use the post_parent__not_in parameter:

$args = array( 
           'post_type'           => 'cpt',
           'post_parent__not_in' => array( 0 ) 
);
$query = new WP_Query( $args );

to retrieve child posts of type cpt.

The generated SQL will then include this part:

wp_posts.post_parent NOT IN (0)

Leave a Comment