• Resolved ryansigg

    (@ryansigg)


    I have a hierarchical custom post type, and on each single view of a post of this type I want to display the full title, content, etc. from the children of this post. This is outside of the main Loop.

    However, the code below has very strange results; the post_content and the_ID are from the correct child-post, but the_title and the_permalink are from the parent post, like this:

    <h1><a href="parentpermalink.php">Parent Title</a></h1>
    <p>Parent Content</p>
    <p>Parent Meta</p>
    <h1><a href="parentpermalink.php" id="post-first-child">Parent Title</a></h1>
    <p>First Child Content</p>
    <h1><a href="parentpermalink.php" id="post-second-child">Parent Title</a></h1>
    <p>Second Child Content</p>

    Using get_children instead of get_posts has the same results:

    global $post;
    $parent = $post->ID;
    
    $children = get_posts('post_parent='.$parent.'&&post_type=cruise&&post_status=publish');
    foreach($children as $child) :
    setup_postdata($child);
    ?>
    <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>">
        		<?php the_title(); ?></a></h2>
     			<?php the_content();
      			endforeach; ?>

    Any help would be much appreciated.
    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi Ryansigg.

    I don’t know if you found a solution for this or if it’s too late now, but have you tried this:

    <?php wp_reset_query(); ?>
    <?php
    global $post;
    
    $loop = new WP_Query(array('post_type' => 'cruise', 'post_status' => 'publish'));
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    	<h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>">
    	<?php the_title(); ?></a></h2>
     	<?php the_content();
    endforeach; ?>
    chadvonlind

    (@chadvonlind)

    Thanks for that, Vayu. Would you know how to exclude children from a hierarchical post type loop?

    $loop = new WP_Query(array('post_type' => 'cruise', 'post_status' => 'publish'));

    I figure this would include the argument that I need. But I don’t see anything under query_posts or WP_query that would allow for that.

    Hi Chadvonlind,

    your welcome. 🙂

    This is one way I found that seems to work:

    <?php
    $args = array(
    	'post_type' => 'cruise',
    	'posts_per_page' => -1,
    	'post_status' => 'publish',
    	'order' => 'ASC',
    	'post_parent' => 0
    );
    $loop = new WP_Query( $args ); ?>

    All top parents have have a post_parent id of zero.

    Hope this works for ya! 🙂

    Vayu

    chadvonlind

    (@chadvonlind)

    That works great! Can I buy you a beer? Do you have a paypal account?

    You are very welcome chadvonlind! It’s very kind of you to offer, but I am just glad I could help. 🙂

    Have good weekend.

    Vayu

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get children of Custom Post Type’ is closed to new replies.