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

---

# [Plugin: Posts 2 Posts] Looping the loop without WP_Query?

 *  Resolved [diniscorreia](https://wordpress.org/support/users/diniscorreia/)
 * (@diniscorreia)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/)
 * Hey there,
 * I’m using Posts 2 Posts and the Zoninator plugin (to edit content “zones” on 
   my theme).
 * Zoninator works with sort of a custom query (similar to get_posts(), if I understand
   it correctly):
 *     ```
       $zone_posts = z_get_posts_in_zone( 'homepage', array( 'numberposts' => 1, 'post_type' => 'any' ), false );
   
       foreach( $zone_posts as $post )  {
       	setup_postdata($post);
       	get_template_part( 'content', get_post_format() );
       }
       ```
   
 * This returns the posts I have assigned to the ‘homepage’ zone.
 * Since there’s no WP_Query going on, is it possible to get the connected posts
   using Posts 2 Posts?
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

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

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703328)
 * With the [development version](http://downloads.wordpress.org/plugin/posts-to-posts.zip)(
   1.3-alpha3), you can just pass the array of post objects to WP_Query or to get_posts():
 *     ```
       $connected = get_posts( array(
         'connected_items' => $zone_posts,
         'connected_type' => array( 'CONNECTION_TYPE1', 'CONNECTION_TYPE2', ... ),
         'connected_direction' => 'any',
         'post_type' => 'any',
         'suppress_filters' => false
       ) );
       ```
   
 * This should return all the connected posts involved.
 *  Thread Starter [diniscorreia](https://wordpress.org/support/users/diniscorreia/)
 * (@diniscorreia)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703346)
 * Thanks you so much! That was exactly what I was trying to achieve.
 * By the way, can I use $connected on p2p_list_posts?
 * Thanks, again.
 * **EDIT:** Hmm just tried it – obviously, if I use `p2p_list_posts($connected)`
   on the loop I get the same list of post for every post on the loop :\
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703471)
 * I just moved the list splitting logic from `each_connected()` into a standalone
   function in the [development version](http://downloads.wordpress.org/plugin/posts-to-posts.zip).
   Usage example:
 *     ```
       $connected = get_posts( array(
         'connected_items' => $zone_posts,
         'connected_type' => array( 'CONNECTION_TYPE1', 'CONNECTION_TYPE2', ... ),
         'connected_direction' => 'any',
         'post_type' => 'any',
         'suppress_filters' => false
       ) );
   
       p2p_distribute_connected( $zone_posts, $connected, 'connected' );
   
       foreach ( $zone_posts as $post ) {
         setup_postdata($post);
   
         the_title();
   
         p2p_list_posts( $post->connected );
   
         ...
       }
       ```
   
 *  Thread Starter [diniscorreia](https://wordpress.org/support/users/diniscorreia/)
 * (@diniscorreia)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703476)
 * Yay, thanks again! Works as expected. *looks for donate button on your site* 
   🙂
 * Another quick question: if I set `'connected_type' => array( 'posts_to_posts','
   posts_to_galleries', 'posts_to_videos')` it only lists posts_to_galleries and
   _to_videos – however, if I only set `'connected_type' => array( 'posts_to_posts')`
   it does lists the connections from that type. Could this be a bug or expected
   behavior, since it’s an indeterminate connection type?
 * Thank you so much again.
 * **EDIT:** Actually, it seems the problem occurs everytime I set more than two
   connection types on that array.
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703477)
 * I forgot one thing about the get_posts() call: `'nopaging' => true`. See if that
   helps.
 * > *looks for donate button on your site*
 * If you can’t find it, it means I suck at design. 🙂
 *  Thread Starter [diniscorreia](https://wordpress.org/support/users/diniscorreia/)
 * (@diniscorreia)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703478)
 * Ah, that was it!
 * By the way, how can that new function still be used in a normal WP Query? (I 
   have some pages on the project that don’t rely on Zoninator).
 * Thanks! And just kicking, the button is very easily found 🙂
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703479)
 * It just takes two arrays of posts:
 *     ```
       p2p_distribute_connected( $my_query->posts, $connected, 'connected' );
       ```
   
 *  Thread Starter [diniscorreia](https://wordpress.org/support/users/diniscorreia/)
 * (@diniscorreia)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703480)
 * Hmm I’m using the global `$wp_query` like this:
 *     ```
       $connected = new WP_Query ( array(
         'connected_items' => $wp_query->posts,
         'connected_type' => array( 'posts_to_posts', 'posts_to_fotogalerias', 'posts_to_videos'),
         'connected_direction' => 'any',
         'post_type' => 'any',
         'nopaging' => true,
         'suppress_filters' => false
       ) );
   
       p2p_distribute_connected( $wp_query->posts, $connected, 'connected' );
       ```
   
 * …but it only lists one connection for one connection type (even with `nopaging`
   set) and I get some warnings (`Notice: Trying to get property of non-object in[...]/
   wp-content/plugins/posts-to-posts/core/api.php on line 339`).
 * Thanks!
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703481)
 *     ```
       p2p_distribute_connected( $wp_query->posts, $connected->posts, 'connected' );
       ```
   
 *  Thread Starter [diniscorreia](https://wordpress.org/support/users/diniscorreia/)
 * (@diniscorreia)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703482)
 * Ah, brilliant.
 * Also, silly me making a new `WP_Query`for `$connected` – it works if I do it 
   with get_posts.
 * Thanks again for all the help, donation on its way.

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

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

 * 10 replies
 * 2 participants
 * Last reply from: [diniscorreia](https://wordpress.org/support/users/diniscorreia/)
 * Last activity: [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-without-wp_query/#post-2703482)
 * Status: resolved