Want more features on Pastebin? Sign Up, it's FREE!
Guest

Posts 2 Posts 1.4.1 each_connected bug

By: certainlyakey on Sep 6th, 2012  |  syntax: PHP  |  size: 1.01 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. //Functions.php
  3. function connect_posts2artists() {
  4.         // Make sure the Posts 2 Posts plugin is active.
  5.         if ( !function_exists( 'p2p_register_connection_type' ) )
  6.                 return;
  7.  
  8.         p2p_register_connection_type( array(
  9.                 'name' => 'posts_to_artists',
  10.                 'from' => 'post',
  11.                 'to' => 'artist'
  12.         ) );
  13. }
  14. add_action( 'wp_loaded', 'connect_posts2artists' );
  15. ?>
  16.  
  17. <?php
  18. //Template file
  19.                 $recentPosts = new WP_Query(array('showposts' => 5, 'post_type' => array('artist')));
  20.                 p2p_type( 'posts_to_artists' )->each_connected( $recentPosts );
  21.                         while( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>
  22. //...
  23. <?
  24.                 // Display connected posts
  25.                 if ($post->connected) {echo '<ul>';}
  26.                 else {echo '<p>This artist has got no connected news items yet</p>';};
  27.                 foreach ( $post->connected as $post ) : setup_postdata( $post );
  28. ?>
  29.                 <li><a href="<? the_permalink(); ?>"><? the_title(); ?></a></li>               
  30. <?              endforeach;
  31.                 if ($post->connected) {echo '</ul>';};
  32.                 wp_reset_postdata(); // set $post back to original post
  33. ?>
clone this paste RAW Paste Data