wp query – WP_Query: “post_parent” and “post_type” combination returning strange results


I’m experiencing some interesting results using post_parent along with post_type (local, WP 5.8):

I have a custom post type project (with 'hierarchical' => true) where I’m grabbing all child posts of a parent on a single view using WP_Query.

When setting post_type to project while setting post_parent I get the same results as if I had not set a post_parent at all: that is, I get all top-level posts with a custom post type of project.

If I set the post_type to any then I get the correct child posts. I have already checked to make sure that the $post->ID for the parent is correct (and not 0) and that the posts are all of the same custom post type. I’ve also double checked the DB for weirdness and verified the parent-child relationships.

To make this stranger, this is on my local; the previous code (setting post_parent AND post_type) works fine on a live/production site (though that’s on an earlier WP version).

This is the query:

$args = array(
    'post_type'   => 'project',
    'post_status' => 'publish',
    'post_parent' => $post->ID,
    'posts_per_page' => -1,
    'orderby' => 'menu_order title',
    'order' => 'ASC'

$projects = new WP_Query( $args );
if( $projects->have_posts() ) :
    while( $projects->have_posts() ) :

As described, if I change to 'post_type' => 'any', then I get the correct child post(s).

What am I missing? Is my data corrupted (though I can’t see any evidence of this)…

rsigg 3 weeks 2021-08-27T11:34:05-05:00 0 Answers 0 views 0

Leave an answer