Title: [Plugin: Posts 2 Posts] connecting from within a nested foreach loop
Last modified: August 19, 2016

---

# [Plugin: Posts 2 Posts] connecting from within a nested foreach loop

 *  Resolved [10sexyapples](https://wordpress.org/support/users/10sexyapples/)
 * (@10sexyapples)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-connecting-from-within-a-nested-foreach-loop/)
 * Hi Scribu,
    You always seem to come up with exactly the impossible code I need
   just as I need it … I’m working with your new posts2posts code over here on a
   new site and of course really loving it … the taxonomy relationships just weren’t
   going to work and when you switched this over to have its own table … yay … answer
   to my prayers … I reworked the whole structure of the site to implement it … 
   hitting a little snag today on the following though so wanted to see if I could
   get your input:
 * Here’s the scenario:
 * I have these connections: ( snippet )
    `p2p_register_connection_type( ‘product’,’
   solution’); p2p_register_connection_type( ‘solution’,’product’);`
 * On my product sidebar I am using the code below to bring in a list of connected
   solutions separated by the taxonomies they are in:
 *     ```
       global $post;
   
       $taxonomies = get_object_taxonomies('solution');
   
       foreach( $taxonomies as $taxonomy ) :
   
       $taxinfo =get_taxonomy($taxonomy);
   
       echo $taxinfo->label;
   
       $solution_query = new WP_Query( array(
       'post_type' => 'solution',
       'taxonomy' => $taxonomy->name,
       /*'connected_from' => $post->ID, */ ) );
   
       if($solution_query->have_posts()) :
   
       echo '<ul>';
   
       while($solution_query->have_posts()):$solution_query->the_post();
       $output ='';
       $output .= '<li><a href="' . get_permalink($post->ID) . '">';
       $output .= $post->post_title;
       $output .= '</a></li>';
       echo $output;
   
       endwhile;
   
       echo '</ul>';
   
       endif;
       endforeach;
       ```
   
 * notice that I have commented out the connected_from parameter?
 * With it commented out, the query returns as it should with the taxonomy headings
   correct and the for each working as it should … But if I uncomment the connected_from,
   I lose the foreach capability.
 * Any ideas why the foreach won’t work with the connected_from in there?
 * As always your input is highly valued and anticipated …

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

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-connecting-from-within-a-nested-foreach-loop/#post-1764160)
 * By “loosing the foreach capability” I assume you mean no posts are found.
 * Instead of ‘connected_from’, try using ‘connected’.
 * Also, instead of:
 *     ```
       p2p_register_connection_type( 'product','solution');
       p2p_register_connection_type( 'solution','product');
       ```
   
 * Try:
 * `p2p_register_connection_type( 'product', 'solution', true);`
 * This defines a reciprocal connection.
 *  Thread Starter [10sexyapples](https://wordpress.org/support/users/10sexyapples/)
 * (@10sexyapples)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-connecting-from-within-a-nested-foreach-loop/#post-1764174)
 * I had tried using just connected previously, but, went ahead and changed to using
   `
   p2p_register_connection_type( 'product', 'solution', true);` and just connected
   instead of connected_from again, but same result … no foreach, and by no foreach
   I mean no foreach 😉 heh. The posts come through properly.
 * Without the connection I get all of the posts in the taxonomy and each of the
   taxonomy headers in place above each list.
 * Without the connection I get the first taxonomy header, then a list of the connected
   posts as expected, and then the rest of the taxonomy headers.
 * I deduced that to mean the query is running properly,
    the while is while working,
   but not the foreach.
 * The only directive ( I lack any knowledge of the proper terminology .. sorry )
   based on the foreach is to give me each of the taxonomies names in a header, 
   and that’s the only directive that doesn’t work when I make the connection active.
 * It works for the first loop through, then breaks and lists the connected posts
   and then goes back to giving me the headers. So, it appears to be interrupting
   the foreach somehow.
 * I’m stumped.
 *  Thread Starter [10sexyapples](https://wordpress.org/support/users/10sexyapples/)
 * (@10sexyapples)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-connecting-from-within-a-nested-foreach-loop/#post-1764322)
 * I still haven’t been able to get this sorted out … have you come across anyone
   else that has wanted to show the connected posts separated by taxonomies?
 * I implemented a dropdown select similar to the date fields meta data example,
   but, soon realized that a meta data type workaround wasn’t going to work in my
   situation. Separate taxonomies have to be the way on this instance.
 * I feel like I’m must be missing something really obvious, but, I’ve yet to work
   out what it is … will keep trying. I’ve checked all over the forums and such 
   but, your code is so new the only real examples are here, and I haven’t found
   any others trying to do this … so … if you hear any workable solution before 
   I find it pretty please will you post it back here? 😉
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-connecting-from-within-a-nested-foreach-loop/#post-1764328)
 * I think I got it:
 * When you call `$solution_query->the_post();` the global variable $post is stomped.
 * So then, when it gets to `'connected__from' => $post->ID` the second time, it’s
   not the post you’re expecting.
 * The solution would be to call wp_reset_postdata() after endwhile.
 *  Thread Starter [10sexyapples](https://wordpress.org/support/users/10sexyapples/)
 * (@10sexyapples)
 * [15 years ago](https://wordpress.org/support/topic/plugin-posts-2-posts-connecting-from-within-a-nested-foreach-loop/#post-1764393)
 * Thanks Scribu … I didn’t get a chance to test, but, it certainly sounds like 
   it would function correctly with this. Marking resolved … sorry it took me so
   long.

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

The topic ‘[Plugin: Posts 2 Posts] connecting from within a nested foreach 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/)

 * 5 replies
 * 2 participants
 * Last reply from: [10sexyapples](https://wordpress.org/support/users/10sexyapples/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/plugin-posts-2-posts-connecting-from-within-a-nested-foreach-loop/#post-1764393)
 * Status: resolved