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]
- Get the ID of the latest post
- How do you get the count of posts in an archive page?
- How to show Y number of custom posts after every X normal posts?
- Can I have Post Types under other Post Types in Admin menu?
- Archive page…limiting posts per page
- Adding custom post type to loop
- Display Custom Post Type Fields
- Custom permalinks – post type – hierarchical taxonomy’s
- Using new WP_Query in shortcode in a custom field causes the main post content to not display
- Taxonomy posts on Archive page
- How to show related posts by category or custom post type?
- Getting taxonomy terms used by custom post type
- How to get only one category of custom post type?
- How to display recent posts added in several custom post types
- Posts navigation in custom post type single.php not working
- Show hierarchical relationship between to custom post types on admin list
- Display custom field of specific post where post title matches variable
- Custom query shows custom post types in trash
- Remove duplicated values from a loop
- Twenty Fifteen: Adjacent posts by menu_order [duplicate]
- How to connect two custom post types with nested loops
- Normal pages as children of my Custom Post Type
- Custom Post Type Loop throws 500 error when used in widget
- Query/list all terms and their custom post count
- What is the recommended / best way to do this: Event calendar post/type in a block-based theme?
- Displaying CPT and custom taxonomy side by side in Bootstrap 4 component
- How to create custom url structure based on custom post type rewrite slug, taxonomy and category slug?
- Group CPT posts by custom taxonomy
- Store CPT ‘Reviews’ average ratings to a WordPress DB table or to a DB custom table?
- Polylang non-default language ignores tags in WP_Query
- Problem: wp_query outputs all images on site
- The loop seems stuck to a single (now deleted) post
- Custom Post Types strange pagination problem
- Custom post type archive page for multiple post types
- Display related custom taxonomy posts in sidebar
- How to display Related Posts based on number of taxonomy terms matched
- Targeting custom post type
- Using wp_query to modify the loop in index.php for a CPT
- Custom WP_Query doesn’t display all posts
- Query & the_content() is showing in the header
- Listing custom post types on archive page with array
- get_terms() parent, child and grandchild
- List child categories from parent category on custom taxonomy page
- Looping through custom taxonomy and display custom post types (Custom Post Type UI)
- Custom post class, generate unique id from 1 to x depending on amount of posts?
- Create a custom taxonomy template that loops through child categories in term order?
- Why is the output of a call to the_excerpt different when the call seems to be identical?
- Displaying Posts Using a Custom Query with a Custom Field and a term_id
- Only looping through pages that have children or subpages
- Query all posts under one taxonomy? Why is my code not working?
- Query for specific taxonomy that executes a particular loop depending on volume of posts?
- creating a foreign key like relationship with custom post types
- How to Enable Ascending or Descending In WordPress Default Built-in Loop
- Fetch data from two custom post types and create multidimensional array for output to html table
- How to sort WP_Query by a custom text field (written as a date dd/mm/yyyy)
- Loop all post on single.php
- Limit custom post type to the authors only on front-end!
- Accessing download link from the loop with WP Download Manager Pro
- Custom WordPress theme not displaying posts from category
- Combine multiple separate lists into one
- Custom Post Type parent/child relationship rewrite rules for permalinks
- custom post type single page
- Most efficient way of showing children posts?
- Custom post types not displaying per category
- How to show Y number of custom posts after every X normal posts?
- WordPress custom post type
- How to loop custom post type posts by author?
- Inserting random posts
- Custom Loop and Standard Blog Loop Issue
- Custom loop won’t work, can’t find problem
- Loop to pull content from parent element in custom post type [duplicate]
- Inserting two categories
- How to create custom page templates with default page layout framework?