据我所知,你的要求,我想这就是你所需要的。
我已经记录了代码,请检查一下。
<?php
/*
Idea: Collecting all the posts child posts by --->\'post_parent__not_in\' => array( 0 )<---
in wp_query. Then find their parent posts by eliminating duplicates.
*/
$query_array = array(
//Change this post type to your Custom-Post-Type.
\'post_type\' => \'news\',
//Showing all posts
\'posts_per_page\' => -1,
//Giving all child posts only
\'post_parent__not_in\' => array( 0 )
);
$the_query = new WP_Query($query_array);
//Array to collect all parent posts
$collect_parents = array();
while($the_query->have_posts()):
$the_query->the_post();
//if condition is used to eliminate duplicates, generated by same child post of parent.
if(!in_array($post->post_parent, $collect_parents)){
//$collect_parents contains all the parent post id\'s
$collect_parents[] = $post->post_parent;
}
endwhile;
//Printing all the parent posts
foreach($collect_parents as $parent){
?>
<!-- Printing parent post title -->
<h2><a href="<?php echo get_permalink($parent ); ?>"> <?php echo get_the_title($parent); ?></a></h2>
<!-- Printing parent post content -->
<p><?php echo get_post_field( \'post_content\', $parent); ?></p>
<?php
}
?>