Title: [Plugin: Posts 2 Posts] Display Connection inside a function that uses $wpdb
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Display Connection inside a function that uses $wpdb

 *  [dhechler](https://wordpress.org/support/users/dhechler/)
 * (@dhechler)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-display-connection-inside-a-function-that-uses-wpdb/)
 * Hey Scribu (or anybody else who can help),
    I’m using a $wpdb->get_results();
   query along with setup_postdata($post); to get posts from the database. This 
   is all inside of a function in my functions.php file. Great, that works just 
   fine. Displays the correct posts and everything.
 * What isnt working is any posts2posts connections inside of the setup_postdata(
   $post);
    Now before you say “use $post->ID ” for the connection item, I have 
   tried this. Here’s my current setup.
 * This registers a post 2 post connection and connects one custom post type (band)
   to another (show).
 *     ```
       p2p_register_connection_type( array(
       		'name' => 'band_to_shows',
       		'from' => 'band',
       		'to' => 'show'
       	) );
       ```
   
 * I know there are connections made with each show because I see them all in the
   backend.
 * The goal of this is to display the connected band on each show post.
 * Now, for the function. This function uses your current location (found elsewhere)
   and gets the latitude and longitude, compares that to the latitude and longitude
   of the postmeta in the database and returns the results if the post is within
   240 miles of you.
 * [http://pastebin.com/L4X60bdn](http://pastebin.com/L4X60bdn)
 * That function works just fine. It’s the loop-shows.php that isnt working with
   posts2posts for some reason. (SEE BELOW FOR CODE SNIPPET).
 *     ```
       <?php
       foreach($nearby_shows as $post): setup_postdata( $post );
       ?>
       <li class="show" data-distance="<?php echo round($distance); ?>">
       	<div class="showinfo">
       <?php
       // THE CODE BELOW DOES NOT ECHO OUT ANYTHING
       $connected = p2p_type( 'band_to_shows' )->get_connected( $post->ID );
               while ( $connected->have_posts() ) : $connected->the_post();
                     the_title();
              endwhile;
       }
       the_title();
       endforeach();
       ?>
       ```
   
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

The topic ‘[Plugin: Posts 2 Posts] Display Connection inside a function that uses
$wpdb’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/posts-to-posts_7a8e9d.svg)
 * [Posts 2 Posts](https://wordpress.org/plugins/posts-to-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/posts-to-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/posts-to-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/posts-to-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/posts-to-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/posts-to-posts/reviews/)

## Tags

 * [posts2posts](https://wordpress.org/support/topic-tag/posts2posts/)
 * [setup_postdata](https://wordpress.org/support/topic-tag/setup_postdata/)

 * 0 replies
 * 1 participant
 * Last reply from: [dhechler](https://wordpress.org/support/users/dhechler/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-display-connection-inside-a-function-that-uses-wpdb/)
 * Status: not resolved