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

I have a WordPress site that relies on relationships quite heavily. It’s a record label website where artists can be related to tours, reviews, album releases and store items throughout the site.

I found the plugin Posts to Posts by Scribu which seems to do what I want, but I don’t understand how to use it properly. Scribu has posted example code, but for some reason I just cannot understand it.

Read More

Here is an example of what I want to do with the site I am working on.

Tours Page.

A tour item has a title and a custom meta field called ‘tickets_link’

Upon loading the tours page, all tours will be shown with their assigned artist and if it has a tickets link, then show that as well.

If someone here can understand the code posted on Scribu’s site for the plugin and then explain it to me, I would gratefully appreciate it.


Here is my code from the functions.php file registering the connections:

    function my_connection_types() {
    if ( !function_exists('p2p_register_connection_type') )
        return;

    p2p_register_connection_type( 'tours', 'artists' );
    p2p_register_connection_type( 'homepage_carousel', 'artists' );
    p2p_register_connection_type( 'duka', 'artists' );
    p2p_register_connection_type( 'products_carousel', 'artists' );
}
  add_action('init', 'my_connection_types', 100);

Pseudo code to show what I want to do in my code:

  • Fetch all tours in my tours custom post archive using a loop that gets all tours added to the site.
  • Each tour is related to an artist, so display all tours for a particular artist.
  • Items are in the form of a heading (artist name) and each item is a tour that has been related to a particular artist in the backend using the metabox that shows as a result from registering a connection.

Further update along with code I am using and print_r values.

I have the related artists showing up in my print_r contents of my query where before I didn’t. When I do a print_r I get the following that shows up, but no way of accessing the data.

[connected_connected] => Array ( [0] => stdClass Object ( [ID] => 245 [post_author] => 1 [post_date] => 2011-03-10 13:55:23 [post_date_gmt] => 2011-03-10 13:55:23 [post_content] => [post_title] => 1200 Techniques [post_excerpt] => [post_status] => publish [comment_status] => closed [ping_status] => closed [post_password] => [post_name] => 1200-techniques [to_ping] => [pinged] => [post_modified] => 2011-03-10 13:55:23 [post_modified_gmt] => 2011-03-10 13:55:23 [post_content_filtered] => [post_parent] => 0 [guid] => http://localhost/testdev/?post_type=artists&p=245 [menu_order] => 0 [post_type] => artists [post_mime_type] => [comment_count] => 0 [p2p_id] => 2 [p2p_from] => 215 [p2p_to] => 245

The code I am using to relate artists with tours is the following:

$connected = new WP_Query(array(
    'post_type' => 'tours',
    'nopaging' => true,
    'each_connected' => array(
        'post_type' => 'artists',
        'nopaging'  => true,
    ),
    'suppress_filters' => false
));

while( $connected->have_posts() ): $connected->the_post();
    the_title();
    echo "<br />";
endwhile;   

print_r($connected); // Print_r for showing contents of post object.

Update for Scribu:

$args = array
(
    'post_type'        => 'tours',
    'nopaging'         => true,
    'suppress_filters' => false
);

$connected = new WP_Query($args);

while($connected->have_posts()): $connected->the_post();

    the_title();
    echo "<br />";

    foreach ($connected->connected AS $tour_item)
    {
        echo get_the_title($tour_item->ID);
        echo "<br />";
    }

endwhile;

Related posts

Leave a Reply

1 comment

  1. 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