WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have a custom post type called "Performers". This gets populated with different bands/performers. These posts have a featured image as well as custom fields (mp3 file, facebook link, myspace link, etc).

I have another custom post type called "Events".

When I create a new Event post, I would like the option to have a drop box to select one of the bands from the "Performers" custom post type.

This will insert all data from the specific Band/Performer into the Event post (custom fields, featured image, etc.).

What is the best method for inserting/injecting this sort of loop from the "Event" admin?

share|improve this question
up vote 3 down vote accepted

Currently the best way I know to handle that is the Posts 2 Posts plugin:

Here's an example showing how to set up the custom post types (if you already have them it's more for other's benefit who might be reading this) as well as the function call to p2p_register_connection_type() needed by the plugin to set up the post relationships. This can can go in your theme's functions.php file or in a .PHP file for a plugin you might be writing:

function event_performer_init() {
      'label'           => 'Events',
      'public'          => true,
      'show_ui'         => true,
      'query_var'       => 'event',
      'rewrite'         => array('slug' => 'events'),
      'hierarchical'    => true,
      //'supports'      => array('title','editor','custom-fields'),
      'label'           => 'Performers',
      'public'          => true,
      'show_ui'         => true,
      'query_var'       => 'performer',
      'rewrite'         => array('slug' => 'performers'),
      'hierarchical'    => true,
      //'supports'      => array('title','editor','custom-fields'),
  if ( function_exists('p2p_register_connection_type') )
    p2p_register_connection_type( 'event', 'performer' );

  global $wp_rewrite;
  $wp_rewrite->flush_rules(false);  // This only needs be done first time

Then within your theme's template file single-event.php you can add code like the following to display information about each Band (I showed the basics here; I'll leave for you to fill in all the details and/or to ask other more specific questions here on the WordPress Answers site such as if you need to know how to get the featured image, etc.)

  if (count($performers = p2p_get_connected($post->ID))) {
    foreach($performers as $performer_id) {
      $performer = get_post($performer_id);
      echo 'The Band: ' . apply_filters('the_title',$performer->post_title);
      echo 'Facebook Link: ' . get_post_meta($post->ID,'facebook_link',true);
share|improve this answer
Thanks Mike, this seems to work pretty well. 2 questions: -have you played with adjusting the order in which the connected posts are queried? (I'm assuming they are default by publish date) -where in the database is this info being stored, is it with the post? I'm trying to do everything I can without the use of plugins as sometimes they store things into their own created database. – Joe Jan 29 '11 at 23:13
@Joe: Don't understand "the order the posts are queried." What's the use-case? What you want requires them to store something in the database; no other reasonable (possible?) way to do it. Even if you don't use a plugin you'll have to store info in the database. The plugin creates two tables wp_p2p and wp_p2pmeta. If you want to see a related in-depth (and heated) discussion on this you can read here. Note that scribu championed the use of the taxonomy system for linking but (without fanfare) he switched to using his own linking table. – MikeSchinkel Jan 29 '11 at 23:22
Mike: Sorry for not clarifying- by "order" I mean attaching multiple bands to a single event. :) – Joe Jan 29 '11 at 23:33
@Joe - Absolutely, that's why I have a loop so that you can loop through the multiple bands that are attached. – MikeSchinkel Jan 29 '11 at 23:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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