Several of my pages have some "posts" of custom post types as child posts. On these pages I want to list all of the child posts. Therefore I have this code:

$page_id = get_queried_object_id();
function subpage_peek($parent_id) {
    // reset the query, just to be sure
    //query subpages
    $args = array(
                 'orderby' => 'menu_order',
                 'sort_order' => 'ASC',
                 'hierarchical' => 0,
                 'post_parent' => $parent_id,
                 'post_type' => 'kuenstler'
    $subpages = new WP_query($args);

    // create output
    if ($subpages->have_posts()) :
        $output = '<ul>';
        while ($subpages->have_posts()) : $subpages->the_post();
            $output .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
        $output .= '</ul>';
    else :
        $output = '<p>No subpages found.</p>';

    // reset the query

    // return something
    return $output;
echo subpage_peek($page_id);

Why do this output all of my posts of the custom post type?

Update #1

This is my solution and it works:

  global $post;
  $post = get_post(get_the_id());
  $child_posts = types_child_posts('kuenstler');
  foreach ($child_posts as $child_post) {
    echo '<li><a href="'.get_permalink($child_post->ID).'">'.get_the_title($child_post->ID).'</a></li>';

Does anyone know which kind of reset I have to use if I want of course the above code to be in the content body, but also in several sub-navigations which lists all of these sub-posts? At the moment the main loop (see above) works, but in each of my four sub-navigations not the children of page x are listed but the subpages of the main loop.

  • Shouldn't $page_id be $parent_id? Or the other way around..
    – Nicolai
    Dec 2 '13 at 19:26
  • Actually these are the same, you are right. But because $parent_id is used inside the function, I used a different name to avoid messing it up.
    – luke
    Dec 2 '13 at 19:34
  • Yes, sorry, doesn't make a difference anyways.. BTW WP_Query doesn't have an argument hierarchical either.
    – Nicolai
    Dec 2 '13 at 19:40

I think you're confusing WP_Query with taxonomy arguments. WP_Query doesn't have a child_of argument, use post_parent. and sort_column should be orderby.

  • I tried this, but now $subpages doesn't have posts. In the backend I linked them definitely. A possible problem could be that if I go to /parent-page/child-custom-post-type I am redirected to /kuenstler/child-custom-post-type. kuenstler is my custom post type
    – luke
    Dec 2 '13 at 19:39
  • 1
    how do you link a custom post type to a page? there's no built-in way to do that unless you've done it manually or added your own code to do so.
    – Milo
    Dec 2 '13 at 19:48
  • I used the plugin "WP Types" and checked the option "hierachichal". This should be the same as if I did it manually with register_post_type(). I wanted to fix the permalink problem after this is solved. But perhaps there is a connection?
    – luke
    Dec 2 '13 at 20:05
  • 1
    I think you'll need to have a look at the WP Types documentation for how to query your related posts. Setting a post type to hierarchical only lets you set a parent of the same post type.
    – Milo
    Dec 2 '13 at 20:11

My final solution:

  Modified function from wp types with capability of choosing post_status
  Originally located here: plugins/types/embedded/functions.php lines 319-342
function mod_wpcf_pr_post_get_has($post_id, $post_type_q = null, $post_status) {
    $post_type = get_post_type($post_id);
    $has = array_keys(wpcf_pr_get_has($post_type));
    $add = is_null($post_type_q) ? '&post_type=any' : '&post_type=' . $post_type_q;
    $posts = get_posts('numberposts=-1&post_status=' . $post_status . '&meta_key=_wpcf_belongs_'
            . $post_type . '_id&meta_value=' . $post_id . $add);

    $results = array();
    foreach ($posts as $post) {
        if (!in_array($post->post_type, $has)) {
        $results[$post->post_type][] = $post;
    return is_null($post_type_q) ? $results : array_shift($results);

$children = mod_wpcf_pr_post_get_has($id, $cpt_slug, 'publish');
foreach ($children as $child_post) {
  if(is_single($child_post->ID) ) { $current = ' class="current"'; }
  echo '<li><a href="'.get_relative_permalink($child_post->ID).'"'.$current.'>'.get_the_title($child_post->ID).'</a>';

I hope I can help some people with this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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