Title: [Plugin: Posts 2 Posts] Looping the loop function help
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] Looping the loop function help

 *  Resolved [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/)
 * Hello! 🙂
 * I’ve succesfully applied the “less efficient” code listed on page [Looping-The-Loop](https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop).
 * But i can’t get to work the second one, the efficient code!
 * I have defined in functions this code:
 *     ```
       function my_connection_types() {
       	// Make sure the Posts 2 Posts plugin is active.
       	if ( !function_exists( 'p2p_register_connection_type' ) )
       		return;
   
       	// Keep a reference to the connection type; we'll need it later
       	global $my_connection_type;
   
       	$my_connection_type = p2p_register_connection_type( array(
       		'id' => 'posts2pages',
       		'from' => 'custom_post_type_1',
       		'to' => 'custom_post_type_2'
       	) );
       }
       add_action( 'init', 'my_connection_types', 100 );
       ```
   
 * Then I have a page where I already have a loop and where i’ve placed the first“
   less efficient” code:
 *     ```
       <?php
       /*
       Template Name: Name
       */
       ?>
   
       <?php get_header(); ?>
   
       <div>
           <h5>Heading name</h5>
           <ul>
           <?php $args = array('post_type'=> 'custom_post_type_1', 'posts_per_page'=> -1); ?>
           <?php query_posts($args); ?>
           <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
               <li>
                   <a href="<?php the_permalink(); ?>">
                       <?php the_post_thumbnail('label-thumb'); ?>
                   </a>
       	    <div>
       	        <h2>
                           <a href="<?php the_permalink(); ?>">
                               <?php the_title(); ?>
                           </a>
                       </h2>
       		<?php
       		    // Find connected pages
       		    $connected = $my_connection_type->get_connected( $post->ID );
       		    // Display connected pages
       		    p2p_list_posts( $connected );
       	        ?>
       	    </div>
       	</li>
       	<?php endwhile; endif; wp_reset_query(); ?>
            </ul>
       </div>
   
       <?php get_footer(); ?>
       ```
   
 * This page template show the connected pages, but out of be less efficient, it
   wraps the connected pages into an extra ‘ul’ and each page link into a ‘li’ with
   his relative anchor ‘a’.
 * Probably i haven’t yet a great php knowledge but i would to display the connected
   pages only as anchor ‘a’ without wrapping them into a ‘li’ list.
 * Then i’ve tried to put into this template page the second “more efficient” code
   but i have an error showing into the page:
 * > Fatal error: Call to a member function each_connected() on a non-object in 
   > address-bla-bla.php on line 25
 * I would be glad if someone can help me!
    Thank you!
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

Viewing 12 replies - 1 through 12 (of 12 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-looping-the-loop-function-help/#post-2369841)
 * Please fix your formatting. I can’t understand anything.
 * Also, use pastebin.com if it’s a big chunk of code.
 *  Thread Starter [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369842)
 * I’m doing it of course, can’t understand myself why the text renders in this 
   way. I’m trying to be the more accurate as possible describing the problem!
 * Ok done! 🙂
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369843)
 * Please paste the code that containes the each_connected() call. It should be 
   right after query_posts().
 *  Thread Starter [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369846)
 * This is what give me error, but i admit that i’m not sure that i’ve placed the
   code in the right way, sounds strange to me the nested whiles.
 *     ```
       <?php get_header(); ?>
   
       <div>
           <h5>Heading Name</h5>
           <ul>
       	<?php $args = array('post_type'=> 'custom_post_type_1', 'posts_per_page'=> -1); ?>
       	<?php query_posts($args); ?>
       	<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
       	<li>
       	    <a href="<?php the_permalink(); ?>">
                       <?php the_post_thumbnail('label-thumb'); ?>
                   </a>
       	    <div>
       		<h2>
                           <a href="<?php the_permalink(); ?>">
                               <?php the_title(); ?>
                           </a>
                       </h2>
       		<?php
       		    // Find connected pages (for all posts)
       		    p2p_type( 'posts_pages' )->each_connected( $wp_query );
       		?>
       		<?php while ( have_posts() ) : the_post(); ?>
       		<?php the_title(); ?>
                       <?php
       		    // Display connected pages
                           p2p_list_posts( $post->connected );
       		?>
       		<?php endwhile; ?>
       	    </div>
       	</li>
       	<?php endwhile; endif; wp_reset_query(); ?>
           </ul>
       </div>
   
       <?php get_footer(); ?>
       ```
   
 * Thank you for the replies!
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369862)
 * It gives you an error because when you define the connection type you use ‘posts2pages’
   and in the template file you use ‘posts_pages’.
 *  Thread Starter [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369882)
 * Yes, thank you, you’re right but you missed another error that is in the second
   example that iv’e posted.
    I’ve discovered it myself when refreshing the page
   after i modified the connection_type name (posts2pages).
 * Other than using a different defined name (posts2pages vs posts_pages), by copying
   your code from your example page i’ve placed into the template also a nested 
   while loop that bring a never endind loop.
    So i had to delete a portion of the
   code that i posted in the 2nd example.
 * This is the working correct code (to be inline i will call this the 3rd example!
   😉 ):
 *     ```
       <?php
       /*
       Template Name: Your Name
       */
       ?>
   
       <?php get_header(); ?>
   
       <div>
           <h5>Heading name</h5>
           <ul>
       	<?php $args = array('post_type'=> 'custom_post_type_1', 'posts_per_page'=> -1); ?>
               <?php query_posts($args); ?>
               <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
       	<li>
       	    <a href="<?php the_permalink(); ?>">
                       <?php the_post_thumbnail('label-thumb'); ?>
                   </a>
       	    <div>
       	        <h2>
                           <a href="<?php the_permalink(); ?>">
                               <?php the_title(); ?>
                           </a>
                       </h2>
       		<?php
       		    // Find connected pages (for all posts)
       		    p2p_type( 'posts2pages' )->each_connected( $wp_query );
       		?>
       		<?php
       		    // Display connected pages
       		    p2p_list_posts( $post->connected );
       		?>
       	    </div>
       	</li>
       	<?php endwhile; endif; wp_reset_query(); ?>
           </ul>
       </div>
   
       <?php get_footer(); ?>
       ```
   
 * I hope that can be useful also to other people that encounter the same difficulties
   that affected me!
 * Last thing scribu, what about the connected page result wrapped into an ‘ul’-
   >’li’ markup?
    How i can leave them into ‘a’ tags only?
 * Thank you!
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369891)
 *     ```
       p2p_list_posts( $post->connected, 'before_list=&after_list=&before_item= &after_item=' );
       ```
   
 *  Thread Starter [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369907)
 * Nope, using this code i have the template markup broken.
    ‘ul’ tag will not be
   closed and apart the first two generated ‘li’ i’ll have all the other ‘li’ elements
   placed at the first div level.
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369915)
 * Works for me. Try again (I added some `&`).
 *  Thread Starter [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369917)
 * Purrfect!
    Yes, i confirm that now works flawlessy. The first time that i’ve 
   tried i suspected that something was missing, i’ve did some tests placing values
   after the ‘=’ like null (=”) but, my shame, i totally forgot the ampersands! 
   🙂
 * So, don’t know where you live but, if we ever met, you’ll have a beer from me!(
   or whatever else drink you like!)
 * If you agree i’ll set the post as solved!
 * Please add some snippets from this thread also in your docomentation/examples,
   could be useful for someone else!
 * Thank you for all!
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369939)
 * Done: [https://github.com/scribu/wp-posts-to-posts/wiki/Using-p2p_list_posts%28%29](https://github.com/scribu/wp-posts-to-posts/wiki/Using-p2p_list_posts%28%29)
 *  Thread Starter [Rics1983](https://wordpress.org/support/users/rics1983/)
 * (@rics1983)
 * [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369943)
 * Great Cristi!
    Top notch! 😉

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

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

 * 12 replies
 * 2 participants
 * Last reply from: [Rics1983](https://wordpress.org/support/users/rics1983/)
 * Last activity: [14 years, 7 months ago](https://wordpress.org/support/topic/plugin-posts-2-posts-looping-the-loop-function-help/#post-2369943)
 * Status: resolved