0

I am currently trying to grab a child page id from a custom post type, but my values returned are always null. I know that the particular ID I'm passing has a parent so there should be a match.

What is the appropriate way to grab child ids using the parent id?

This is what I have tried:

$parentid = $order->get_id();

$args = array(
  'post_parent'     => $parentid,
  'post_type'       => 'shop_subscription'
);

$child = new WP_Query($args);

if ($child->have_posts()) : while ($child->have_posts()) : $child->the_post();
  $childid = get_the_id();  
endwhile;
else:
  $childid = "not set";
endif;
2

1 Answer 1

2

This should work. I'm not entirely sure what is wrong with your code (it's getting late) but the below code works on my end. Obviously I replaced $order->get_id() with a known ID and post_type was set to page.

<?php 
    $parentid = $order->get_id();

    $child = new WP_Query( array('post_parent' => $parentid, 'post_type' => 'shop_subscription') );

    if ($child->have_posts()) : while ($child->have_posts()) : $child->the_post();
        $childid = get_the_ID();
    endwhile;
        else:
            $childid = "not set";
    endif;

    wp_reset_query();

?>                      

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.