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.

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') )

    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();
    echo "<br />";

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();

    echo "<br />";

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


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

  • That's perfect Scribu, but I'm not using it on single tour items as there isn't going to be single tour items. Basically I am using a custom post archive, is your plugin able to work with custom post archives? My dilemma it would seem is that I don't have an ID to query related posts for. Can I do a query that lets me pull out all tours and their associated artist? Mar 13, 2011 at 9:35
  • I've added the code I am using in my functions.php file as well in-case I messed up something in there as well. Mar 13, 2011 at 9:47
  • Sorry if I seem a little slow, but still doesn't seem to work for me. Where does $post come from? Am I supposed to be populating that with something or is the plugin supposed to be doing that? See my question for the latest code I am trying. I appreciate your help Scribu. Mar 13, 2011 at 11:51
  • Forgot to mention I am getting the error "Warning: Invalid argument supplied for foreach()" when trying to use the $post->connected foreach loop. Mar 13, 2011 at 11:53
  • 1
    Thank you very much Scribu. I got it all working, I appreciate your time and effort on this. Mar 13, 2011 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.