How to exclude posts with parent post in wp_query, WordPress

To exclude posts that have a parent (i.e., child posts) in a WP_Query request, you can use the post_parent argument. This argument controls whether the post has a parent or not. To exclude child posts, set the condition post_parent => 0, which means that only top-level posts (posts without a parent) will be included in the query.

Here’s an example query:

$args = array(
    'post_type' => 'post', // Replace with the desired post type
    'post_parent' => 0,    // Exclude child posts
    'posts_per_page' => -1 // Number of posts to retrieve (adjust as needed)
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // Your code to display posts
    }
} else {
    // No posts found
}
wp_reset_postdata();

The key part here is the post_parent => 0 argument, which excludes all posts that have a parent.

How useful is the publication?

Click on a star to rate it!

Average score 5 / 5. Number of grades: 1

No ratings yet. Rate it first.

Similar posts

How to override –wp–preset–color–black parameter in Gutenberg

In the Gutenberg block editor, you can override the –wp–preset–color–black parameter, which is responsible for the preset black color, using theme filtering or global styles. Ways to modify 1. Through theme.json If your theme supports theme.json, you can override the preset color in the settings.color.palette section. Example: This method automatically changes the value of the…
Read more

jquery how to get link on current site

In jQuery, you can get the current URL of the site using the JavaScript window.location object. Here are a few ways: 1. Full URL: 2. Current host (domain): 3. Current path (without domain): 4. Query string parameters: Using jQuery (optional): jQuery is not required here since access to window.location is provided by standard JavaScript. However,…
Read more

How to get next saturday in php

To get the next Saturday’s date in PHP, you can use the strtotime function with an appropriate string format. Here’s an example: This code: Thus, you’ll get the next Saturday’s date in the YYYY-MM-DD format.
Read more