WordPress.org

Support

Support » Plugins and Hacks » Posts 2 Posts » Connection between 3 post types?

Connection between 3 post types?

  • Hi, I have a site with 3 post types; ‘Contributor’, ‘Book’ and ‘Post’. I’ve set it up so a user can connect a Book to a Contributor and a Contributor to a Post. I’ve got it working so that on a Book page all the connected Contributors are listed and other Books by the same Contributor are listed. On a Contributor page all the Books and Posts by that Contributor are listed.

    What I want is for a user to be able to connect a Contributor to a Post and then on a Book page list all Posts that are connected to any Contributors connected to that book.

    So basically a Book page lists the connected Contributors, other Books by that Contributor(s) AND ALSO any blog Posts by that Contributor(s).

    Is this possible with this plugin? Or should I be looking at another method? My code is below. Thanks!

    <!-- Linked contributors -->
    							<?php
    							// Find connected Contributors
    							$connected = new WP_Query( array(
    							  'connected_type' => 'books_to_contributors',
    							  'connected_items' => get_queried_object(),
    							  'nopaging' => true,
    							) );
    							// Display connected Contributors
    							if ( $connected->have_posts() ) :
    							?>
    							<h3>Contributors:</h3>
    							<ul>
    							<?php while ( $connected->have_posts() ) : $connected->the_post(); ?>
    							    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    							<?php endwhile; ?>
    							</ul>
    							<?php
    							// Prevent weirdness
    							wp_reset_postdata();
    							endif;
    							?>
    
    							<!-- Connected Books by same Contributors -->
    							<?php
    							// Find connected Books
    							$related = p2p_type( 'books_to_contributors' )->get_related( get_queried_object() );
    							// Display connected Books
    							if ( $related->have_posts() ) :
    							?>
    							<h3>Related Books:</h3>
    							<ul>
    							<?php while ( $related->have_posts() ) : $related->the_post(); ?>
    							    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    							<?php endwhile; ?>
    							</ul>
    							<?php
    							// Prevent weirdness
    							wp_reset_postdata();
    							endif;
    							?>

    https://wordpress.org/plugins/posts-to-posts/

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think you only need to run a second loop through the Contributors query, and then with every contributor, do another query to get the Posts from that Contributor.

    I have done something similar in some projects.

    Thank you. I actually ended up going with a slightly different system as the needs of the site changed slightly, but I will bear your answer in mind for future instances of this.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Connection between 3 post types?’ is closed to new replies.
Skip to toolbar