wp query - Get Child Page IDs by Parent ID

admin2025-06-05  10

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;

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;
Share Improve this question asked Nov 16, 2017 at 0:25 elkefreedelkefreed 3851 gold badge5 silver badges16 bronze badges 2
  • The accepted answer here stackoverflow/a/43164688/648252 is what finally helped me solve my issue. – elkefreed Commented Nov 16, 2017 at 18:20
  • use this : codex.wordpress/Function_Reference/get_children – Jignesh Patel Commented Aug 1, 2018 at 13:40
Add a comment  | 

1 Answer 1

Reset to default 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();

?>                      
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749064963a316052.html

最新回复(0)