Support » Plugin: Custom Post Type UI » Parent – child – sibling list for CPT not working

  • Resolved Boris

    (@ispacecrusader)


    Hi,
    I’ve been working with Custom Post Type UI for quite a while, and I’m very happy with it! However, while building a new site, I’m running into a problem I hope you can help me with:

    – I’ve created a CPT called ‘Themes’. It has both Hierarchical and Has Archive set to True.
    – There are a few parent ‘Themes’ and some of them have child ‘Themes’.
    – In the sidebar, I want to show a dynamic list: for both the parent and the children, I want to show all of the child (for the parent) and sibling (for the children) ‘Themes’. So the same last for both parent and child.
    – I have a code snippet that works fine for regular pages and child pages. But when I use it for the CPT, the results are empty both on the parent and child pages. Here is the code:

    
    

    <?php
    if($post->post_parent){
    // get a list of all the children of the parent page
    $pages = get_pages(array(‘child_of’=>$post->post_parent));

    if ($pages) {
    // get the ids for the pages in a comma-delimited string
    foreach ($pages as $page)
    $page_ids[] = $page->ID;
    $siblings = implode(‘,’,$page_ids);

    // $pages_to_get is a string with all the ids we want to get, i.e. parent & siblings
    $pages_to_get = $post->post_parent.’,’.$siblings;

    // use “include” to get only the pages in our $pages_to_get
    $children = wp_list_pages(“include=”.$pages_to_get.”&echo=0″);
    }

    }
    else{
    // get pages that direct children of this page: depth=1
    $children = wp_list_pages(“title_li=&child_of=”.$post->ID.”&depth=1&echo=0″);
    }

    // display the children:
    if ($children) {
    echo ‘<h4>Explore ‘ . get_the_title($parent[1]) . ‘</h4>’;
    echo $children;
    }
    ?>`
    `

    I’ve tested with either post or page capability type, but the problem remains the same. I even deleted the entire CPT and recreated it with page instead of post capability, but to no avail.

    Can you help me get this working?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    Hard to say exactly what’s not working quite right here, cause visually it looks fine to me so far. The question is more what’s coming back from these queries.

    In this type of case, I’d likely start from the top and work my way down, confirming each section of the code. For example, start with the very first get_pages. Are you receiving results? Are they the results you’d be expecting? I yes, keep going. Does your $siblings variable end up holding everything expected in the comma-separated string? If yes, keep going and so on. This will help identify exactly where the results aren’t what you’re expecting any more and where to start troubleshooting.

    Thread Starter Boris

    (@ispacecrusader)

    Hi Michael,
    thanks for your help!!

    I’ve put in var_dumps, like this:

    
    

    <?php
    if($post->post_parent){
    // get a list of all the children of the parent page
    $pages = get_pages(array(‘child_of’=>$post->post_parent));

    echo var_dump($pages) . “<br>”;

    if ($pages) {
    // get the ids for the pages in a comma-delimited string
    foreach ($pages as $page)
    $page_ids[] = $page->ID;
    $siblings = implode(‘,’,$page_ids);

    echo var_dump($siblings) . “<br>”;

    // $pages_to_get is a string with all the ids we want to get, i.e. parent & siblings
    $pages_to_get = $post->post_parent.’,’.$siblings;

    echo var_dump($pages_to_get) . “<br>”;

    // use “include” to get only the pages in our $pages_to_get
    $children = wp_list_pages(“include=”.$pages_to_get.”&echo=0″);

    echo var_dump($children) . “<br>”;
    }

    }
    else{
    // get pages that direct children of this page: depth=1
    $children = wp_list_pages(“title_li=&child_of=”.$post->ID.”&depth=1&echo=0″);
    }

    // display the children:
    if ($children) {
    echo ‘<h4>Explore ‘ . get_the_title($parent[1]) . ‘</h4>’;
    echo $children;
    }
    ?>`
    `

    The result is:
    – no results whatsoever on the parent CPT
    – on the child CPT, the only result is: array(0) { }

    Thread Starter Boris

    (@ispacecrusader)

    I think the problem might be with the $post->post_parent.

    I originally used different code because I only wanted to see the child CPT’s on the parent’s sidebar. That worked fine using this code:

    
    

    <?php
    global $post;
    $args = array(
    ‘post_parent’ => $post->ID,
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘any’,
    );

    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) :
    ?>

    <ul class=”projects”>
    <?php
    while ( $the_query->have_posts() ) : $the_query->the_post();
    ?>

    • “><?php the_title(); ?>
    • <?php
      endwhile;
      endif;
      wp_reset_postdata();
      ?>
      `
      `

      But as soon as I wrapped it in an IF statement that checked whether it was a parent, I got an empty result.

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    I guess my biggest question at this point is right to the very top of the code you’re showing. Is this $post variable object the correct one you’re wanting to use, and is it actually a child of something, or is it the parent itself.

    $pages = get_pages(array(‘child_of’=>$post->post_parent));
    

    If the post you’re viewing is the top most in this hierarchy, then you’d want to change that snippet above to be “child of” $post->ID to get that viewed post’s children.

    Thread Starter Boris

    (@ispacecrusader)

    Hi Michael,

    I’m not sure what you mean with your first sentence. But I changed the code
    (‘child_of’=>$post->post_parent)
    to
    (‘child_of’=>$post->ID)
    and it makes no difference on either the top most page or the child pages.

    If I change the IF statement to
    if($post->ID)

    I get the same result as on the child pages:
    array(0) { }

    I’m completely at a loss here…

    Plugin Author Michael Beckwith

    (@tw2113)

    The BenchPresser

    As mentioned in one of my earlier replies, best I can suggest here is working your way down, and confirming you’re managing to get what you’re expecting at each step.

    For example, first make sure that $post is representing the post you need, and then make sure you’re passing that post’s ID to the corresponding function that you’re expecting to get the children for. Then check and see that you’re getting the accurate children, and keep moving forward.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.