How Do I Use The WordPress Plugin Posts 2 Posts by Scribu?

Ok, so the idea is that you have an outer loop, that displays the tours.

And then you have an inner loop, that displays each artist.

The way The Loop works is that it populates a lot of global variables, such as $post, so it looks like magic.

Let’s look at a more uniform approach:

$tours = get_posts( array(
    'post_type' => 'tours',
    'nopaging' => true,
    'each_connected_to' => array(
        'post_type' => 'artists',
        'nopaging' => true,
    ),
    'suppress_filters' => false
) );

// outer loop
foreach ( $tours as $tour ) {
    echo get_the_title( $tour->ID );
    echo get_post_meta( $tour->ID, 'ticket_link', true );

    // inner loop
    foreach ( $tour->connected_to as $artist ) {
        echo get_the_title( $artist->ID );
        echo '<br/>';
    }
}

Update: This answer is obsolete; for a current example, see https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop

Leave a Comment