Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress + PHP: Query posts to find those that contain a custom field array which contains a specific key
    primarykey
    data
    text
    <p>I have a two post types: artists and events.</p> <p>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.</p> <p>On the events page I show the list of artists for each event with the following:</p> <pre><code> //Get Event Artists $postID = $post-&gt;ID; $meta = get_post_meta($postID, $_artistMeta-&gt;$postID, TRUE); $artists = unserialize($meta['_artistMeta'][0]); $output = array(); foreach ($artists['artistName'] as $artist) { $theArtist = get_post($artist); $output[] = '&lt;li&gt;&lt;a href="'.get_permalink($theArtist-&gt;ID).'"&gt;'.$theArtist-&gt;post_title.'&lt;/a&gt;&lt;/li&gt;'; } &lt;?php if($output) { ?&gt; &lt;div class="tw-event-artists bg1"&gt; &lt;ul class="nav-inl group me c2 comma-list"&gt; &lt;li class="bo"&gt;&lt;?php echo $featured; ?&gt;:&lt;/li&gt; &lt;?php echo implode('', $output); ?&gt; &lt;/ul&gt; &lt;/div&gt; &lt;?php } ?&gt; </code></pre> <p>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.</p> <p>I need to create a custom query to find event posts whose artists array contains the current artist slug.</p> <p>I think <a href="http://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/" rel="nofollow">http://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/</a> that tutorial is a good starting place but my PHP chops are simply not good enough to hack it.</p> <p>How can write this query to get what I want?</p> <p>Thanks!</p> <hr> <p>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.</p> <pre><code>&lt;?php $artistID = $post-&gt;ID; $args = array( 'eventDisplay'=&gt; 'upcoming', 'posts_per_page' =&gt; -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-&gt;ID; $meta = get_post_meta($postID, $_artistMeta-&gt;$postID, TRUE); $artists = unserialize($meta['_artistMeta'][0]); foreach ($artists['artistName'] as $artist) { if($artist == $artistID) {$has_date = true;} } endforeach; endif; ?&gt; &lt;?php if($has_date) {echo 'HAS DATE';} ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload