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.
Leave a Reply
You must be logged in to post a comment.
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.
You must be logged in to post a comment.
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.
You can just add
post_parent=0
to your queryElaborating from @Ryan’s post, the key is setting
post_parent=0
andpost_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: