WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get children of Custom Post Type (6 posts)

  1. ryansigg
    Member
    Posted 3 years ago #

    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.

  2. Vayu Robins
    Member
    Posted 3 years ago #

    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; ?>
  3. chadvonlind
    Member
    Posted 3 years ago #

    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.

  4. Vayu Robins
    Member
    Posted 3 years ago #

    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

  5. chadvonlind
    Member
    Posted 3 years ago #

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

  6. Vayu Robins
    Member
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic