WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Using P2P Results to Display Another P2P Connection (4 posts)

  1. Andrew.Bloom
    Member
    Posted 1 year ago #

    Having a bit of trouble displaying output.

    I have three custom post types: products, brands, and distributors.

    I want to be able to display distributors info on the products page(s).

    My relationships are: products_to_brands and brands_to_distributors.

    Any help would be appreciated, am I missing something (I don't have too much experience with this plugin)?

    $args =  array(
    	  'connected_type' => 'products_to_brands',
    	  'connected_items' => get_queried_object()
    	);
       $connected = new WP_Query($args);
    
    while ( $connected->have_posts() ) :     $connected->the_post();
    
       $args =  array(
    	  'connected_type' => 'brands_to_distributors',
    	  'connected_items' => get_queried_object()
    	);
       $connected = new WP_Query($args);
    
    while ( $connected->have_posts() ) :     $connected->the_post();
    
    the_title();
    
    endwhile;
    
    endwhile;

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    Examples of nested loops can be found in the wiki:

    https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop

  3. Andrew.Bloom
    Member
    Posted 1 year ago #

    $my_query = new WP_Query( array(
    		'post_type' => array(
    					'products',
    					'brands',
    					'distributors'
    				) )
    			);
    
    p2p_type( 'products_to_brands' )->each_connected( $my_query, array(), 'brands' );
    
    while ( $my_query->have_posts() ) : $my_query->the_post();
    
    	// Another level of nesting
    	p2p_type( 'brands_to_distributors' )->each_connected( $post->brands, array(), 'distributors' );
    
    	foreach ( $post->brands as $post ) : setup_postdata( $post );
    		echo '<h3>Connected Distro</h3>';
    
    		foreach ( $post->distributors as $post ) : setup_postdata( $post );
    			the_title();
    		endforeach;
    	endforeach;
    
    	wp_reset_postdata();
    endwhile;

    I used an example, still no output.

    I know I'm doing something wrong. I'm going to continue working on it until I get it right.

  4. Andrew.Bloom
    Member
    Posted 1 year ago #

    I got it now, took a look at some older posts in this support forum.

    Made a temporary ID number for the query to work off of:

    <?php
    $wp_query = new WP_Query( array(
      'connected_type' => 'products_to_brands',
      'connected_items' => get_queried_object(),
      'nopaging' => true,
    ) );
    
    if ( $wp_query->have_posts() ) :
    
    	$temp_brand_ID = "";
    
    while ( $wp_query->have_posts() ) : $wp_query->the_post();
    
    	$temp_brand_ID = get_the_ID();
    
    $connected = new WP_Query( array(
    	'connected_type' => 'brands_to_distributors',
    	'connected_items' => $temp_brand_ID,
    	'nopaging' => true,
    	'orderby' => 'title',
    	'order' => 'ASC'
    ) );
    ?>
    <h2>Distributors:</h2>
    <ul>
    <? while ( $connected->have_posts() ) : $connected->the_post(); ?>
    	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <? endwhile; ?>
    </ul>
    	<?
    
    endwhile;
    	wp_reset_postdata();
    endif;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic