Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Post to Post Shortcode (plugin wordpress)

By: FrancoisL on Apr 12th, 2012  |  syntax: PHP  |  size: 0.96 KB  |  views: 82  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. `
  2. <?php
  3. /**
  4. * Plugin Name: Display Posts to Posts
  5.  
  6. // Create the shortcode
  7. add_shortcode(‘p2p’, ‘display_post2post’);
  8.  
  9. function display_post2post($atts) {
  10.  
  11. extract( shortcode_atts( array(
  12. ‘relation_post’ =>  ”,  //posts_to_pages or relationship targeted
  13.                         // other attributes to pass?
  14. ), $atts ) );
  15.  
  16. $args = array(
  17. ‘relation_post’ => $connected_type,
  18. ‘connected_type’ => $connected_type,
  19. ‘connected_items’ => get_queried_object_id(),
  20. ‘nopaging’ => true,
  21. // other filters ?
  22. );
  23.  
  24. $return = ";
  25. $connected = new WP_Query($args);
  26. if ( $connected->have_posts() ):
  27. $return .= "<h3>Related posts:</h3>;
  28. $return .= "
  29. <ul>";
  30. while ( $connected->have_posts() ): $connected->the_post();
  31. $return .= "
  32. <li><a>"><?php the_title(); ?></a></li>
  33. ";
  34. endwhile;
  35. $return .= "</ul>
  36. ";
  37.  
  38. // Prevent weirdness
  39. wp_reset_postdata();
  40. $return .= ”;
  41. endif; wp_reset_query();
  42.  
  43. if (!empty($return)) return $return;
  44. }
  45. ?>
  46. `