I have a two post types: artists and events.
I have associated the artists and events by putting a check list of all artists in the event editor, so that multiple artists can be associated with multiple events. The artist custom field on the event editor is saved as an array of artist post IDs.
On the events page I show the list of artists for each event with the following:
//Get Event Artists
$postID = $post->ID;
$meta = get_post_meta($postID, $_artistMeta->$postID, TRUE);
$artists = unserialize($meta['_artistMeta'][0]);
$output = array();
foreach ($artists['artistName'] as $artist) {
$theArtist = get_post($artist);
$output[] = '<li><a href="'.get_permalink($theArtist->ID).'">'.$theArtist->post_title.'</a></li>';
}
<?php if($output) { ?>
<div class="tw-event-artists bg1">
<ul class="nav-inl group me c2 comma-list">
<li class="bo"><?php echo $featured; ?>:</li>
<?php echo implode('', $output); ?>
</ul>
</div>
<?php } ?>
The problem I'm trying to find now is to try and "go in reverse". So in my artists archive, I want to find the latest event for each artist.
I need to create a custom query to find event posts whose artists array contains the current artist slug.
I think http://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/ that tutorial is a good starting place but my PHP chops are simply not good enough to hack it.
How can write this query to get what I want?
Thanks!
My current method of achieving this is like so, placed inside the loop on my artists archive template, but I imagine this will be extremely slow and inefficient because it has to get every single event for every artist.
<?php
$artistID = $post->ID;
$args = array(
'eventDisplay'=> 'upcoming',
'posts_per_page' => -1
);
$events = tribe_get_events($args);
$has_date = false;
if ($events):
global $post;
foreach ($events as $post):
setup_postdata($post);
//Get Event Artists
$postID = $post->ID;
$meta = get_post_meta($postID, $_artistMeta->$postID, TRUE);
$artists = unserialize($meta['_artistMeta'][0]);
foreach ($artists['artistName'] as $artist) {
if($artist == $artistID) {$has_date = true;}
}
endforeach;
endif;
?>
<?php if($has_date) {echo 'HAS DATE';} ?>