Title: [Plugin: Posts 2 Posts] Loop over post 2 post loop
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Loop over post 2 post loop

 *  Resolved [argeee](https://wordpress.org/support/users/argeee/)
 * (@argeee)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-loop-over-post-2-post-loop/)
 * Hi Scribu,
    On a sidebar I am using your p2p to show the products related to 
   that of the main area. Now, I want to have a loop of places where you can buy
   the product by creating a loop on top of the posts 2 posts loop. The code is 
   as follows:
 *     ```
       $wp_query = new WP_Query(array(
          'post_type' => 'client',
          'client_type' => 'Distributor'));
          while ($wp_query->have_posts()) : $wp_query->the_post();
           echo '<h2>' . the_title() . '</h2>'; //just testing if outputs
          endwhile;
       wp_reset_postdata();
       ```
   
 * The posts 2 posts works only if I remove the above code. However, If I put this
   code under the posts 2 post code they both come out.
 * thanks again for your help,
 * Mauro

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

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-loop-over-post-2-post-loop/#post-2348755)
 * See [https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop](https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop)
 *  Thread Starter [argeee](https://wordpress.org/support/users/argeee/)
 * (@argeee)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-loop-over-post-2-post-loop/#post-2348848)
 * Hi Scribu,
    I don’t thinks that I explained my question correctly. The first 
   loop is not a p2p loop. Meaning its a wp loop. The second one is a p2p loop. 
   My problem is that if I do a WP_Query on before the p2p, p2p outputs nothing,
   whereas if I do it after everything is OK. Please see a summarized version of
   my code:
 *     ```
       $wp_query = new WP_Query(array(
          'post_type' => 'client',
          'client_type' => 'Distributor'));
          while ($wp_query->have_posts()) : $wp_query->the_post();
           echo '<h2>' . the_title() . '</h2>'; //just testing if outputs
          endwhile;
       wp_reset_postdata();
   
       $connected = new WP_Query( array(
       	'post_type' => 'ups',
       	'connected_from' => get_the_ID(),
       	'nopaging' => true,
       	'suppress_filters' => false
       ) );
       ?>
       <div id="products_wrapper">
       	<h3><?php _e('you may also be interested in...','eq_power') ?></h3>
       	<?php
       	// if ($connected->have_posts()) :
       	while( $connected->have_posts() ) : $connected->the_post();
       	?>
       	<div class="products_item_wrapper">
       		<?php  echo '<h2>' . the_title() . '</h2>'; //just testing if outputs?>
       	</div>
       	<!-- end products_item_wrapper -->
       	<?php endwhile;
       		wp_reset_postdata();?>
   
       <!-- end products_wrapper -->
       ```
   
 * thanks,
 * Mauro
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-loop-over-post-2-post-loop/#post-2348850)
 * The problem is that get_the_ID() is meant to be used within The Loop.
 * Try replacing it with get_queried_object_id() or storing it in a temporary variable
   before the non-p2p query.
 *  Thread Starter [argeee](https://wordpress.org/support/users/argeee/)
 * (@argeee)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-loop-over-post-2-post-loop/#post-2348853)
 * Yep, it worked. I used the following:
    `$temp_prod_ID= get_queried_object_id();`
   and then substituted get_the_ID() as follows:
 *     ```
       $connected = new WP_Query( array(
       	'post_type' => 'ups',
       	'connected_from' => $temp_prod_ID,
       	'nopaging' => true,
       	'suppress_filters' => false
       ) );
       ```
   
 * PS.
    How you give better support than for most paying plugins is something that
   is beyond me…
 * Anyway, I made a small contribution for all the help that you’ve been giving 
   me.
 * Thanks again,
 * Mauro

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

The topic ‘[Plugin: Posts 2 Posts] Loop over post 2 post loop’ 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/)

 * 4 replies
 * 2 participants
 * Last reply from: [argeee](https://wordpress.org/support/users/argeee/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-loop-over-post-2-post-loop/#post-2348853)
 * Status: resolved