Zubair Mushtaq
Zubair Mushtaq

Reputation: 323

Plugin Post 2 Posts: How to show the latest connected child post from parent post in loop?

I want to list latest child connected post ordered by parent post. Is it possible? Could anyone help me with that? I explain with an example: let's say I have Dramas custom post type and an Episode custom post type, that are connected to Posts 2 Posts. In the Drama archive, I want to list all the dramas by their latest episode and want to show only one episode in the loop.

I think it is maybe possible with each_connected(), but I don't know how to order the $wp_query array to get this done.

Thanks.

I am using with such kind of code now

<?php
global $post;

$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;

$anime_series = array(
        'post_type' => 'drama',
        'posts_per_page' => 12,
        'paged' => $paged,
        'page' => $paged,
        'post_status' => 'publish',
        'orderby'        => 'post_date',
        'order'          => 'DESC'
        );        

$the_query = new WP_Query( $anime_series);

$extra = array(
    'posts_per_page' => 1,
    'post_status' => 'publish',
    'orderby'        => 'post_date',
    'order'          => 'DESC'
    );      

p2p_type( 'drama_to_episode' )->each_connected( $the_query, $extra, 'episode' );
?>
</div>

<div class="last_episodes loaddub">
  <ul class="items">
    <?php if ( $the_query->have_posts() ) : ?>
    <?php  while ($the_query->have_posts()) : $the_query->the_post(); ?>
    <li>
      <div class="img"> <a href="<?php echo the_permalink(); ?>" title="<?php echo the_title(); ?>"> <img alt="<?php echo the_title(); ?>" itemprop="photo" src="<?php echo get_the_post_thumbnail_url(); ?>"> </a>
        <div class="type"><img src="<?php echo  get_stylesheet_directory_uri(); ?>/images/SUB.png"></div>
      </div>
      <?php 


            foreach ( $post->episode as $post ) : setup_postdata( $post );  ?>
      <p class="name"> <a href="<?php echo the_permalink(); ?>" title="<?php echo get_the_title($items['parent']); ?>"> <?php echo get_the_title($items['parent']); ?> </a> </p>
      <p class="episode"> <?php echo the_field('short_name');  ?> </p>
      <?php 

            endforeach; 

            ?>
    </li>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php else:  ?>
    <p>
      <?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?>
    </p>
    <?php endif; ?>
  </ul>
</div>

Upvotes: 0

Views: 180

Answers (2)

Zubair Mushtaq
Zubair Mushtaq

Reputation: 323

Luckily, I solved my problem myself. My primary concern was to get the latest connected child post with its parent. Before in default loop, all the episodes (old/new) were being retrieved. I made a custom select query for Posts to Posts plugin. This query returned the IDs of the latest connected post. The working code is given below. Kindly, correct me if I am using the wrong programming standards.

global $post, $items, $wpdb; 

$table_name = $wpdb->prefix . "p2p";
$query = 'SELECT p2p_from as anime, max(p2p_to) as episode FROM '.$table_name;

$results = $wpdb->get_results( $query.' GROUP BY p2p_from ORDER BY episode DESC' );

Then, in foreach loop, I retreived the post title, permalink etc by the ID.

Upvotes: 0

Priyanka Modi
Priyanka Modi

Reputation: 1624

Try this easy example

?php
 $args = array(
 	'post_parent' => 38,
 	'post_type'   => 'page',
 	'numberposts' => -1,
 	'post_status' => 'any'
 );
 $children = get_children( $args );
 foreach($children as $post):
	 echo $post->post_title;
endforeach;
 ?>

Hope that work it in your requirement!!!

Upvotes: 0

Related Questions