Question

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?

Was it helpful?

Solution

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:

add_action('init','event_performer_init');
function event_performer_init() {
  register_post_type('event',
    array(
      'label'           => 'Events',
      'public'          => true,
      'show_ui'         => true,
      'query_var'       => 'event',
      'rewrite'         => array('slug' => 'events'),
      'hierarchical'    => true,
      //'supports'      => array('title','editor','custom-fields'),
    )
  );
  register_post_type('performer',
    array(
      '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.)

<?php
  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);
    }
  }
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top