I'm building a site with several post types and am using a single index.php
to query the archive for each one in turn. One of these post types is hierarchical and I'd like to just query the parent. I can use query_posts
to limit to 'post_parent' => 0
but that creates a new query and resets the post type argument meaning that I'd have to create separate queries for each post type.
Is there a way to limit while ( have_posts() ) : the_post();
to parents only without creating a new query?
pre_get_posts
hook? It can help you modify the main query.query_posts
, if you ever need to query for posts, useWP_Query
instead, or at the very least,get_posts
. If you ever need to modify what posts appear on a page or archive, always usepre_get_posts
.query_posts
exists because its removal would break a lot of websites.