$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display a list of child posts on parent posts of a custom post type|Programmer puzzle solving
最新消息: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)

Display a list of child posts on parent posts of a custom post type

matteradmin10PV0评论

I have a hierarchical custom post type called 'products'and I have created a 2 level parent-child relationship. I want to list all the subpages/child posts on each parent post but I have tried everything and nothing works.

I tried the page-list plugin and it only works for the default 'Page' post type. Same with the tutorial found on wpbeinner titled "How to Display a List of Child Pages For a Parent Page in WordPress". Both work for default page type but not the custom post types.

Would appreciate some input. Thanks

I have a hierarchical custom post type called 'products'and I have created a 2 level parent-child relationship. I want to list all the subpages/child posts on each parent post but I have tried everything and nothing works.

I tried the page-list plugin and it only works for the default 'Page' post type. Same with the tutorial found on wpbeinner titled "How to Display a List of Child Pages For a Parent Page in WordPress". Both work for default page type but not the custom post types.

Would appreciate some input. Thanks

Share Improve this question asked Apr 1, 2017 at 6:56 AdamAdam 531 gold badge1 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 6

The best way is using WP_Query. I think your error or plugin error could be that the 'post_type' of childs is not define.

WP Query : https://codex.wordpress/Class_Reference/WP_Query

global $post;

$args = array(
    'post_parent' => $post->ID,
    'posts_per_page' => -1,
    'post_type' => 'products', //you can use also 'any'
    );

$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
  // Do Stuff
    the_title();
    echo "<br>";
    the_content();
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
Post a comment

comment list (0)

  1. No comments so far