Title: [Plugin: Posts 2 Posts] Looping the loop.. reversed?
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Looping the loop.. reversed?

 *  Resolved [chocks](https://wordpress.org/support/users/chocks/)
 * (@chocks)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-reversed/)
 * I love this plugin Scribu (yesterday I found the need to buy you a beer and sent
   a donation through your site!)
 * It’d be awesome if you could help me out cracking this bit of functionality: 
   I have two post types: offices and jobs. I want to display the offices that have
   jobs and list them accordingly:
 *     ```
       <li class="drop-list"><span>Office Name 1</span>
                     <ul class="accord-list">
                       <li><a href="link">Job Name 1</a></li>
                       <li><a href="link">Job Name 2</a></li>
                     </ul>
                   </li>
       ```
   
 * The registration connection is:
 *     ```
       p2p_register_connection_type(
       		array(
       		'name' => 'jobs_to_offices',
       		'from' => 'jobs',
       		'to' => 'offices',
       		'reciprocal' => true,
       		'cardinality' => 'many-to-many',
       		'title' => 'Related Jobs',
       		)
       ```
   
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

Viewing 5 replies - 1 through 5 (of 5 total)

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-reversed/#post-2909600)
 * So you read the [Looping The Loop](https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop)
   tutorial.
 * Did you actually try to implement it? What does the code look like?
 *  Thread Starter [chocks](https://wordpress.org/support/users/chocks/)
 * (@chocks)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-reversed/#post-2909769)
 * Thanks for dropping by Scribu! I wrote the following:
 *     ```
       <?php
       	$connected_offices = p2p_type( 'jobs_to_offices' )->get_connected();
       	$show_offices = new WP_Query("post_type=offices&posts_per_page=-1&post__in=$connected_offices");
       	$i=1; while ($show_offices->have_posts()) : $show_offices->the_post();
       ?>
       	<li class="drop-list"><span><?php the_title();?></span>
       	        <ul class="accord-list">
       			<?php
       			$connected = p2p_type( 'jobs_to_offices' )->get_connected( $post );
       			while ( $connected->have_posts() ) : $connected->the_post();
       			?>
       				<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
       			<?php
       			// Prevent weirdness
       			endwhile;
       			wp_reset_postdata();
       			?>
       		</ul>
       	</li>
       <?php endwhile;	?>
       ```
   
 * But it brought me all the offices instead of the ones that actually have active
   connections. I thought that by sending $connected_offices = p2p_type( ‘jobs_to_offices’)-
   >get_connected(); to post__in it would do the trick.. but it didn’t..
 * Any clue?
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-reversed/#post-2909771)
 * Either use get_connected() – which alredy returns a WP_Query instance – or create
   a WP_Query instance manually; not both.
 * Since you want to get all the offices, you have to set the direction explicitly:
 *     ```
       $show_offices = new WP_Query( array(
         'post_type' => 'offices',
         'connected_type' => 'jobs_to_offices',
         'connected_items' => 'any',
         'connected_direction' => 'to',
         'nopaging' => true,
       ) );
       ```
   
 *  Thread Starter [chocks](https://wordpress.org/support/users/chocks/)
 * (@chocks)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-reversed/#post-2909772)
 * Thanks for this scribu!
    I ended up doing this:
 *     ```
       <?php
       	$offices_shown=array();
              	$connected_offices = p2p_type( 'jobs_to_offices' )->get_connected();
       	$show_offices = new WP_Query( array(
       		  'post_type' => 'offices',
       		  'nopaging' => true,
       		  'connected_type' => 'jobs_to_offices',
       		  'connected_items' => 'any',
       		  'connected_direction' => 'from'
       	) );
       	$i=1; while ($show_offices->have_posts()) : $show_offices->the_post();
       	$office_id = $post->ID;
       	if (!isset($offices_shown[$office_id])) :
       	$offices_shown[$office_id]=1;
       ?>
       	<li class="drop-list"><span><?php the_title();?></span>
                     <ul class="accord-list">
                    	<?php
       		$connected = p2p_type( 'jobs_to_offices' )->get_connected( $post );
       		while ( $connected->have_posts() ) : $connected->the_post();
       		?>
       			<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
       		<?php
       		// Prevent weirdness
       		endwhile;
       		wp_reset_postdata();
       		?>
       	     </ul>
       	</li>
       	<?php endif; endwhile;	?>
       ```
   
 * I had to change the direction you set to “from” as the other way around it would
   give me a list of jobs with the connected offices and I wanted to list the offices
   with the connected jobs. Then I had to set an array to hide the offices displayed
   as it would repeat for some reason an office with jobs. In the end I got what
   I was looking for although the ‘array hack’ isn’t my favorite approach!
    Thanks
   a lot,
 * J
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-reversed/#post-2909773)
 * Glad to hear you figure it out. Note that you can remove this line:
 *     ```
       $connected_offices = p2p_type( 'jobs_to_offices' )->get_connected();
       ```
   
 * since you don’t use `$connected_offices` anywhere.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘[Plugin: Posts 2 Posts] Looping the loop.. reversed?’ 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/)

 * 5 replies
 * 2 participants
 * Last reply from: [scribu](https://wordpress.org/support/users/scribu/)
 * Last activity: [13 years, 9 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-reversed/#post-2909773)
 * Status: resolved