Title: [Plugin: Posts 2 Posts] Nested loops relating to one CPT
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Nested loops relating to one CPT

 *  [Arlo](https://wordpress.org/support/users/arlo/)
 * (@arlo)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-nested-loops-relating-to-one-cpt/)
 * I have 3 custom post types: Blurb, Writer, and Article
 * I have 3 p2p’s set up:
    Blurb to Writer Blurb to Article Writer to Article
 * On the single.php of Article, I want to show a list of all the Blurbs related
   to that Article, separated by Writer:
 * Writer 1
    – Blurb a – Blurb b
 * Writer 2
    – Blurb c – Blurb d etc…
 * I can do a single loop of any combination, but trying to nest it so I loop through
   each article’s writer then each writer’s blurb related to the article has been
   frustrating. Any ideas?
 * Thanks for any help
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

Viewing 6 replies - 1 through 6 (of 6 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-nested-loops-relating-to-one-cpt/#post-2740922)
 * You can use each_connected():
 * [https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop](https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop)
 *  [dlukez](https://wordpress.org/support/users/dlukez/)
 * (@dlukez)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-nested-loops-relating-to-one-cpt/#post-2740924)
 * Hi Arlo. Try this in single-article.php:
 *     ```
       // get writers connected to blurbs connected to this article
       $writers = p2p_type( 'blurb_to_writer' )->set_direction( 'to' )->get_related( $post );
   
       // get blurbs connected to this article (by way of being connected to a related writer so that we can use p2p_distribute_connected to merge the arrays)
       $blurbs = p2p_type( 'blurb_to_writer' )->set_direction( 'from' )->get_related( $post );
   
       // match the blurbs with the writers and store in each writer
       p2p_distribute_connected( $writers->posts, $blurbs->posts, 'blurbs' );
   
       echo '<ul>';
       foreach( $writers->posts as $writer ):
       	echo '<li>';
       	echo '<a href="' . get_permalink($writer) . '">' . get_the_title($writer) . '</a>'; // display the title of this writer (with a permalink)
       	p2p_list_posts( $writer->blurbs ); // list blurbs for this writer
       	echo '</li>';
       endforeach;
       echo '</ul>';
       ```
   
 * This should display writers connected to blurbs that are connected to this article.
   It ignores the writer to article relationship since I assumed that would be for
   specifying the author or writers the article talks about, or something ultimately
   unrelated to blurbs.
 *  Thread Starter [Arlo](https://wordpress.org/support/users/arlo/)
 * (@arlo)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-nested-loops-relating-to-one-cpt/#post-2740937)
 * scribu- thanks for that, I did actually go through that page, but really couldn’t
   make sense of it.
    dlukez, thanks so much for the snippet. The loop nesting is
   breaking a bit somehow:
 *     ```
       <ul>
         <li>Writer1</li>
         <li>Writer1
           <ul>
             <li>Blurb A</li>
             <li>Blurb B</li>
           </ul>
         </li>
       </ul>
       ```
   
 * Not only is “Writer1” repeating but I should not be seeing “blurb b”, as that
   is connected to another article. It is connected to Writer1, however, so it seems
   the filtering by the main article loop isn’t working.
 * But one thing I should add, is that p2p_list_posts() won’t work for me in this
   instance, as I need to output custom content for each writer/blurb grouping, 
   not just a link list. I’m wondering if what I want to do is just too complex 
   in general.
 *  [dlukez](https://wordpress.org/support/users/dlukez/)
 * (@dlukez)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-nested-loops-relating-to-one-cpt/#post-2740984)
 * Yeah I find each_connected() confusing in some cases. Could you clarify the relationships
   between Blurb, Writer and Article? I don’t really understand how your connections
   are set up so I probably have mine set up differently!
 *  Thread Starter [Arlo](https://wordpress.org/support/users/arlo/)
 * (@arlo)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-nested-loops-relating-to-one-cpt/#post-2740988)
 * These are my connections:
 *     ```
       p2p_register_connection_type( array(
       		'name' => 'blurb_writer',
       		'from' => 'blurb',
       		'to' => 'writer'
       	) );
   
       	p2p_register_connection_type( array(
       		'name' => 'blurb_article',
       		'from' => 'blurb',
       		'to' => 'articles'
       	) );
   
       	p2p_register_connection_type( array(
       		'name' => 'writer_article',
       		'from' => 'writer',
       		'to' => 'articles'
       	) );
       ```
   
 * I’ve been testing in parallel using just taxonomies to get the job done, but 
   of course this isn’t ideal for the user (they’d need to manually add taxes of
   the same kind in multiple tax-types to achieve what p2p *should* alone).
 *  [dlukez](https://wordpress.org/support/users/dlukez/)
 * (@dlukez)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-nested-loops-relating-to-one-cpt/#post-2741004)
 * So you want to display writers connected to the article, then blurbs connected
   to both that article AND each writer who is also connected to that article?
 * A short description of what each content type and relationship means, then why
   you want to display them on the article might help. e.g. “a blurb is an excerpt
   for one article” (blurb->article).
 * I think p2p can do what you want, it’s more a matter of working out the best 
   way to fit things together.

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

The topic ‘[Plugin: Posts 2 Posts] Nested loops relating to one CPT’ 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

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [nested](https://wordpress.org/support/topic-tag/nested/)

 * 6 replies
 * 3 participants
 * Last reply from: [dlukez](https://wordpress.org/support/users/dlukez/)
 * Last activity: [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-nested-loops-relating-to-one-cpt/#post-2741004)
 * Status: not resolved