Adavnced custom fields relationship query plus query inside

I am currently developing a site, I have three custom post types (speakers, events, videos). Each event has a relationship with speakers so when a event is created they select what speakers spoke at that event. my event page works fine and I can call the speakers fine. The problem I am having in my videos custom post type when creating a video the user has 2 relationships which they select which event it was recorded at and what speaker it is for. So on my events page under each speaker. I want it to get their video for that event. i have tried using meta query’s but I cannot get it to work. I have posted on ACF forum with no reply. Any help would really be appreciated I have tried everything I can think of.

http://support.advancedcustomfields.com/forums/topic/nesting-relationship-querys/

Read More

Above is the post on advanced custom field support forum.

Here is a paste bin of my events.php

http://pastebin.com/LB8bdyKj

Here is the section of code ,

<?php
$posts = get_field('speakers');
if( $posts ) :
foreach( $posts as $post) :
  setup_postdata($post);
?>
<div class="span_6 speaker">
  <div class="span_5">
    <img src="<?php the_field('speakers_image'); ?>" />
  </div>
  <div class="span_7">
  <h3><?php the_title();?></h3>
  <?php
  $videos = get_posts( array(
    'post_type' => 'videos',
    'meta_query' => array(
      'relation' => 'AND',
      array(
       'key' => 'speaker',
       'value' => get_the_ID()
      ),
      array(
       'key' => 'events',
       'value' => $eventid
      )
    )
  ) );
  $videoquerys = new WP_Query($videos);
  ?>
  <a class="" href="#">View Profile</a>
  </div>
</div>

Above is what i have tried the information between event and speaker i can get fine its querying the speaker and event and getting the video that matches i am having trouble with.

Related posts

2 comments

  1. Looks like you’re using both get_posts() and new WP_Query() — You only need one or the other. You’re also using WP_Query() incorrectly — It’s looking for an array of arguments, but you’re passing $videos, which is an array of post objects.

    Try this:

    <?php
    
    $args = array(
      'post_type' => 'videos',
      'meta_query' => array(
        'relation' => 'AND',
        array(
         'key' => 'speaker',
         'value' => get_the_ID()
        ),
        array(
         'key' => 'events',
         'value' => $eventid
        )
      )
    );
    
    $videos = new WP_Query($args);
    
    if($videos->have_posts()) : 
       while($videos->have_posts()) : 
          $videos->the_post();
    
    ?>
    
    <!-- STUFF FOR EACH VIDEO HERE -->
    <a href="<?php the_permalink(); ?>" ?>"><?php the_title(); ?></a>
    
    <?php
    
    endwhile;
    endif;
    
    ?>
    

    Untested. Hope it works for you.

Comments are closed.