Advertisement
Guest User

Untitled

a guest
Sep 21st, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.10 KB | None | 0 0
  1. In functions.php:
  2.  
  3. <?php
  4.  
  5. add_action('init', 'dgo_connection_types', 100);
  6. add_filter('p2p_connectable_args', 'dgo_connectable_places', 10, 2);
  7.  
  8. function dgo_connectable_places($args, $context) {
  9.     if ('place' == $args['post_type']) {
  10.         global $wpdb;
  11.  
  12.         $query = "SELECT $wpdb->p2p.p2p_to AS post_id FROM $wpdb->p2p";
  13.  
  14.         $result = $wpdb->get_col($query);
  15.  
  16.         $args['post__not_in'] = $result;
  17.         $args['orderby'] = 'date';
  18.         $args['order'] = 'ASC';
  19.     }
  20.     return $args;
  21. }
  22.  
  23. function dgo_connection_types() {
  24.     if (!function_exists('p2p_register_connection_type'))
  25.         return;
  26.  
  27.     global $postplaces;
  28.  
  29.     $postplaces = p2p_register_connection_type(array(
  30.         'from' => 'post',
  31.         'to' => 'place'
  32.             ));
  33. }
  34. ?>
  35.  
  36. In index.php:
  37.  
  38. <?php if (have_posts()) : ?>
  39.     <?php
  40.         global $wp_query;
  41.         $postplaces->each_connected($wp_query);
  42.     ?>
  43.     <?php while (have_posts()) : the_post(); ?>
  44.     <?php global $post; ?>
  45.     <?php print_r($post); // $post->connected is not present ?>
  46.     <?php endwhile; ?>
  47. <?php endif; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement