Support

Account

Home Forums Front-end Issues Reverse query relationship field

Helping

Reverse query relationship field

  • I’ve been using the 3rd part of your knowledgebase article: http://www.advancedcustomfields.com/resources/querying-relationship-fields/

    Trying to work out how to setup a query that does the following on a single page:

    1) display all of one post type (clients)
    2) if that post type is attached (by relationship field) to another post type (work), show client title as link to the index of that client page (single-client.php)
    3) if that post type is not attached to another post type (work), show client title as non-link

    My code so far:

    <?php
    	$terms = get_terms( 'practice_area', 'orderby=count&hide_empty=0' );
    	if( !empty( $terms ) ){
    		foreach ( $terms as $term ) {
    			$term_arr = array();
    			$args = array(
    				'post_type' => 'work',
    				'practice_area' => $term->slug,
    				'posts_per_page' > -1
    			);
    			$query = new WP_Query( $args );
    			if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post();
    			  
    			$client = get_field('client');
    			if( !empty( $client ) ){
    				$term_arr[ $term->term_id ][ $client[0]->ID ][] = get_the_ID();								 
    			}	
    			endwhile;endif;
    			
    			if( !empty( $term_arr ) ){
    				foreach( $term_arr as $term_id => $client_data ){
    					$term_data = get_term_by('id', $term_id, 'practice_area');
    					echo '<div class="list">';
    					echo '<h1>' . $term_data->name . ' +</h1>';
    
    					if( !empty( $client_data ) ){										  
    						foreach( $client_data as $client_id => $post_data ){
    						    //echo '<h2>'.get_the_title($client_id).'</h2>';
    							if( !empty( $post_data ) ){	
    								//echo '<ul>';									  
    								foreach( $post_data as $post_id ){
    								 	//echo '<li><a href="'.get_permalink($post_id).'">'.get_the_title($post_id).'</a></li>';
    								}	
    								//echo '</ul>';
    							}
    						 
    						}	
    					}
    
    					wp_reset_query();
    
    					$args = array(
    					    'post_type' => 'client',
    					    'posts_per_page' => '-1',
    					    'practice_area' => $term_data->name,
    					    'order' => ASC
    					);
    
    					query_posts( $args ); ?>
    
    					<ul>
    					<?php while ( have_posts() ) : the_post(); ?>
    
    						<?php if( !empty( $client_data ) ) { ?>
    
    						<li><a href="#"><?php the_title(); ?></a></li>
    
    						<?php } else { ?>
    
    						<li><?php the_title(); ?></li>
    
    						<?php } ?>
    
    					<?php endwhile; // end of the loop. ?>
    					</ul>
    
    					<?php wp_reset_query();
    
    					echo '</div>';
    
    			   }
    		   }
    	   }
    	 }  
    ?>
  • Hi @pilgrimish

    It looks like the last query in your code is the one which is related to your question, is this correct?

    Within this ‘client’ loop, you will need to use the get_posts function to see if any posts exist (‘work’) which contain a custom field value where the current ‘client’ has been selected.

    Is all the rest of your code working?
    If so, the additional get_posts should be quite straight forward. Please read over the doc to understand how you can use compare LIKE to find a value within a serialized array.

    Thanks
    E

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Reverse query relationship field’ is closed to new replies.