In your functions.php you can create a function that will hook into the pre_get_posts hook. Something like (just an example):
function alter_query($query){
$query->set('post_parent', 0);
}
add_action( 'pre_get_posts', 'alter_query' );
There you can alter the main query. That way while(have_posts()) : the_post(); will still just normally work. (You should check if you are altering the right query and such. Otherwise the post_parent will be applied to all query’s by wordpress. You don’t want that).
The BIG advantage is, that if you alter the main query, you only have to do that. WordPress will still select the appriopiate template and such. If you also want to alter the template loaded, you can use the template_redirect hook.
Related Posts:
- Retrieving 3 latest post from each of 5 different custom post types
- Query for posts in 2 taxonomies
- How to get current post id of a custom post type in a loop using template singel-{custom type}.php?
- ‘category__and’ for custom taxonomy?
- Separate Posts and Custom Post Type in Custom Taxonomy archive template
- How to dynamically build a multiple taxonomy query loop within a post type’s single loop?
- How to insert content from another Custom Post type into Post?
- Change or update WordPress loop based on dropdown selection
- Show a Category X’s custom post type on Category X archive page?
- Listing all term items alphabetically / sorting loop
- pagination not working for category.php (custom post types in categories)
- get parent content inside child posts
- 3 random images from custom post type, each in a div with a diffrent class
- Control content before and after custom post type loop
- Pagination not working on custom query on a page
- Loop through posts of only 2 statuses
- postsperpage value not being applied
- Custom post type hierarchical loop in Homepage
- Custom Post type showing up in loop, regular posts are not showing up
- Custom Post Type Query W/Category Dropdown
- Custom Post Types Not Showing Up In query_posts Result
- Can I show some specific post rather than latest post?
- display news with pictures 3 small and one large (loop)
- How do I correctly query posts from a post ID?
- How to exclude certain portfolios from a loop
- Render a loop in Timber (twig for WordPress)
- show custom post’s post in two different divs [duplicate]
- Custom fields (wp_post_meta) vs Custom Table for large amount of data
- Get custom post type’s fields in a while loop as variables
- Custom Post Type within the Loop on Homepage (Page Template)
- Custom while loop for hierarchical display of a taxonomy
- The loop seems stuck to a single (now deleted) post
- Homepage’s content is dependent on the custom field values (set automatically), how do I get homepage to update without manually updating page?
- Get the taxonomy of a post hierarchically
- Query posts based on the meta key values of logged-in users?
- Posts per row on archive page (custom post type)
- Custom Post Types hierarchy and rewrite
- How do I know the ‘meta_key’ for ordering my custom post type
- Custom post type / taxonomy rewrite archive page 2 gives 404
- Limit the post for differents custom post type in the same wp_query
- Custom Theme With Custom Loops
- Custom Post Types strange pagination problem
- Different Ways to Query Custom Post Types?
- Create more category hierarchical depth for custom post type/taxonomy plugin (widget)
- Hierarchial Custom Post Types/Taxonomies
- Hierarchical permalinks for custom post type and taxonomy
- BBPress Search results in WordPress search
- using wp_update_post for updating custom post type
- Custom post type archive page for multiple post types
- Custom post type conditional in loop
- Display related custom taxonomy posts in sidebar
- Custom post type Permalinks with hierarchical Taxonomies
- Only show posts with date of today or in the future (i.e don’t show past posts)?
- Create a permalink structure with custom taxonomies and custom post types gives 404
- How do I control the output of a custom post type in the loop?
- How to display Related Posts based on number of taxonomy terms matched
- Display custom post type posts first, then default posts
- Custom Post Type Category Link
- Custom Post type category pages template and loop
- Three Most Recent Posts, One Per Term
- Targeting custom post type
- Does WP have a global of $id?
- WordPress Loop if/else
- get_the_title() is returning results from previous loop
- Custom Post Loop pulling all custom posts, not just one
- Conditional for a Single Post That Belongs to a Category?
- Wrapping x posts in html without leaving empty html
- WP Query post__in not returning correct results
- Custom Post Type single.php template only shows the latest post
- Sort posts in loop by the WooCommerce Membership of the author
- Filter date from post meta when date is in string format
- query_posts adding extra code to homepage
- Including all post id’s of a custom post type into an array
- How to include custom post type posts on a page?
- How to show only the most recent post on my custom post type archive?
- 4 column-loop ordered alphabetically with entries grouped (and labeled) by their first letter
- Tag page only display 10 posts
- How to make WP_Query not to show irrelevant posts?
- What is the ID parameter for custom post types in query_posts?
- Loop for custom post types filtered by a taxonomy
- How can I group posts by months and years?
- Pagination Error : Duplicate argument being outputted
- get_post_type is always post
- Multiple Custom Post Type queries, how to DRY it up
- Custon Content within WordPress Loop
- Custom Post type & Taxonomy 404
- Trying to Create a PHP Variable for post_type that can be referenced Site Wide
- Bootstrap accordion looping through posts incorrectly
- Main query not querying any posts in custom taxonomy template
- Loop increase number
- Custom post type showing same Post on all Pages
- get_post_meta is not working inside shortcode
- How to limit post per page of custom post type?
- Using wp_query to modify the loop in index.php for a CPT
- the_post() is printing titles in page footer
- Is it safe to change a Custom Post Type from “hierarchical:false” to “hierarchical:true”?
- Custom WP_Query doesn’t display all posts
- Custom query – get_the_terms not work
- Query & the_content() is showing in the header
- Create Array from custom post type to display a slider