最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Get Child Page IDs by Parent ID

matteradmin11PV0评论

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();

?>                      

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far