Support

Account

Home Forums Front-end Issues How to Query Relationship Field Reply To: How to Query Relationship Field

  • Okay, this works. But, holy schnikes, that’s a lot of work for one little list!

    <label>Presenter: </label>
    <?php
    #----------------- GET THE DISTINCT LIST OF SERIALIZED PRESENTERS FROM PRESENTATIONS																		
    $presenter_id_array = array();
    $presenter_data = $wpdb->get_col( 
    "SELECT DISTINCT wp_postmeta.meta_value FROM wp_posts 
    INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    WHERE 1=1 AND wp_posts.post_type = 'presentations' AND (wp_posts.post_status = 'publish') AND (wp_postmeta.meta_key = 'presenters' ) 
    GROUP BY wp_posts.ID ");		
        
    #----------------- LOOP THROUGH THAT LIST										
    foreach ($presenter_data as $presenter_data_item)
    {
        #----------------- DO A QUERY TO GET A SPECIFIC PRESENTATION POST
        $args = array(
            'post_type'	=> 'presentations',
            'posts_per_page'     => -1,					
            'post_status' => 'publish',											
            'meta_query' => array(
              array(
                'key' => 'presenter',
                'value' => $presenter_data_item,
                'compare' => '=',
              )
            )																							
        );																	                                       
        
        $custom_loop = new WP_Query($args);	
        #echo "<br /><br >". $custom_loop->request; 	
        
        #----------------- GET THAT PRESENTATION'S DATA
        if( $custom_loop->have_posts() ):						
        
            while( $custom_loop->have_posts() ): $custom_loop->the_post(); 
            
                #----------------- GET THE PRESENTER RELATIONSHIP FIELD DATA
                $posts = get_field('presenter');
                 
                if( $posts ): 
                    foreach( $posts as $post_object): 
                        
                        #----------------- FINALLY, GET THAT STAFF ID OUT OF THE RELATIONSHIP FIELD!!!!
                        #----------------- ADD IT TO MY LIST OF STAFF IDS
                        array_push($presenter_id_array,$post_object->ID);
                    endforeach; 
                endif;																																																								
            endwhile;        
        endif;																										
    }									
                                        
    #----------------- NOW, DO A QUERY FOR A SUBSET OF STAFF WHO ARE PRESENTERS
    $args = array(
        'post_type'	=> 'staff-members',
        'posts_per_page'     => -1,					
        'post_status' => 'publish',
        'order' => 'ASC',
        'orderby' => 'title',
        'post__in' => $presenter_id_array												
    );																	                                       
    
    $custom_loop = new WP_Query($args);	
    #echo "<br /><br >". $custom_loop->request; 	                                       												
    
    ?><select name="search_presenter">
        <option value="">-</option>
            <?php if( $custom_loop->have_posts() ):						
            
                while( $custom_loop->have_posts() ): $custom_loop->the_post(); 
                    
                    ?><option value="<?php get_the_id(); ?>"><?php the_title(); ?></option><?php
                endwhile;        
            endif;
            
            // Reset Query
            wp_reset_postdata(); ?>
    </select>