Support

Account

Home Forums General Issues Querying relationship fields

Solved

Querying relationship fields

  • I am having some trouble querying relationship fields. Here are the components that I am working with. I have news articles (custom post type = “news-articles”) and then I have investments (custom post type = “as-investments”). Each investment has an image under the field name “investment_logo” and the relationship is pulled on the new articles page under the field name “associations”. I know know how to query relationship fields for a single post but the problem I am running into is that I have a page where I want to query several posts from “news-articles” (which has different types articles, press releases, etc) and get the image from the investment that I choose as the association. I would be forever grateful to someone who can help me. Here is the code as it lies now:

    <div id="content-section" class="<?php the_title(); ?>">
    
    <?php get_sidebar('nav'); ?>
    
    	<?php if (have_posts()) : ?>
    
    		<?php while (have_posts()) : the_post(); ?>        
            
    			<?php americsecur_entry_before(); ?>
    			<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>       
    				<?php americsecur_entry_top(); ?>
    
                    <?php get_template_part( 'post-meta-page' ); ?>
                    
                    <div class="post-entry">
                        <?php the_content(__('Read more ›', 'americsecur')); ?>
                    </div><!-- end of .post-entry -->
    
    <h3>News Articles</h3>
    <?php $args = array(
    	'numberposts' => -1,
    	'post_type' => 'news-articles',
    	'meta_key' => 'release_type',
    	'meta_value' => 'In the News'
    );
    
    $the_query = new WP_Query( $args ); ?>
    <?php if( $the_query->have_posts() ): ?>
    
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<div>
    		
    <a href="<?php the_permalink() ?>" title="<?php printf(__('%s','rys'), get_the_title()) ?>" rel="bookmark" ><?php the_title(); ?></a>
    
    		<em><?php the_field('publication_source'); ?></em>
    
    <span><?php $date = get_field('publication_date');
    			echo date('F d, Y', datepicker_to_unix($date )); ?></span>
    		</div>
    	<?php endwhile; ?>
    <?php endif; ?>
    
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    
    <h3>Press Releases</h3>
    <?php $args = array(
    	'numberposts' => -1,
    	'post_type' => 'news-articles',
    	'meta_key' => 'release_type',
    	'meta_value' => 'Press Releases'
    );
    	
    $the_query = new WP_Query( $args ); ?>
    <?php if( $the_query->have_posts() ): ?>
    
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<div>
    		
    <a href="<?php the_permalink() ?>" title="<?php printf(__('%s','rys'), get_the_title()) ?>" rel="bookmark" ><?php the_title(); ?></a>
    
    		<em><?php the_field('publication_source'); ?></em>
    
    <span><?php $date = get_field('publication_date');
    			echo date('F d, Y', datepicker_to_unix($date )); ?></span>
    		</div>
    	<?php endwhile; ?>
    
    <?php endif; ?>
    
    <?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>
    
    <?php americsecur_entry_bottom(); ?>      
    </div><!-- end of #post-<?php the_ID(); ?> -->
                
    <?php endwhile; 
    get_template_part( 'loop-nav' ); 
    else : 
    get_template_part( 'loop-no-posts' ); 
    endif; 	?>     
    
    </div><!-- end of #content-section -->

    Help me please!

  • Hi @kikimarie1234

    Thanks for the question. There’s a lot to take in here, and I wonder if some screenshots of the post_Type edit screens would help understand what is connected where.

    What is the issue you are having? Do you not know where to put the code?

    I think the solution is quite simple. You need to add the relationship code within you loops. Have you looked over the relationship field documentation code examples?

  • Yes, I am aware that I need to put the relationship code within my loop but I don’t know what the code should be. Every time I tried adding it, I got a php error. I’m confused because I’m already querying multiple different posts on the page (unlike the documentation which shows an example of querying for a single template file).

    Please see the screenshots for what I’m working with. I know this is slightly complicated but I’m sure folks would want to know how to do this as well.

    I want to query the image somewhere between this div.

    <div>
    		
    <a href="<?php the_permalink() ?>" title="<?php printf(__('%s','rys'), get_the_title()) ?>" rel="bookmark" ><?php the_title(); ?></a>
    
    		<em><?php the_field('publication_source'); ?></em>
    
    <span><?php $date = get_field('publication_date');
    			echo date('F d, Y', datepicker_to_unix($date )); ?></span>
    		</div>

    Really appreciate your help! Thank you!!

  • Hi @kikimarie1234

    Thanks for the screenshots.

    The code you want to use will look something like this:

    
    <div>
    		
    	<a href="<?php the_permalink() ?>" title="<?php printf(__('%s','rys'), get_the_title()) ?>" rel="bookmark" ><?php the_title(); ?></a>
    
    	<em><?php the_field('publication_source'); ?></em>
    
    	<span><?php $date = get_field('publication_date'); echo date('F d, Y', datepicker_to_unix($date )); ?></span>
    	
    	<?php 
    	
    	// 1. Get all the relationship values
    	$associations = get_field('associations');
    	
    	
    	// 2. Make sure a value exists
    	if( $associations ): ?>
    		
    		<?php 
    		
    		// 3. loop through relatinoship values
    		foreach( $associations as $association): ?>
    			
    			<?php // 4. Load the image field from the association ?>
    			<img src="<?php echo get_field('investment_logo', $association->ID); ?>" />
    			
    		<?php endforeach; ?>
    
    	<?php endif; ?>
    </div>
    

    Please note this is an example and not the final Markup you will need.

    Thanks
    E

  • Thank you SO much! This is what I ended up with…

    <div>
    		
    		<?php $associations = get_field('associations');
    		if( $associations ): ?>
    		
    		<?php foreach( $associations as $association): ?>
    			<?php 
    			$photo = get_field('investment_logo', $association->ID); ?>
    					
    			<a href="<?php the_permalink() ?>" title="<?php printf(__('%s','rys'), get_the_title()) ?>" rel="bookmark">
    			<img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>" />
    			</a>
    			
    		<?php endforeach; ?>
    
    		<?php endif; ?>
    
    		<a href="<?php the_permalink() ?>" title="<?php printf(__('%s','rys'), get_the_title()) ?>" rel="bookmark" >
    		<?php the_title(); ?></a>
    
    		<em><?php the_field('publication_source'); ?></em>
    
    		<span><?php $date = get_field('publication_date'); echo date('F d, Y', datepicker_to_unix($date )); ?></span>
    
    		</div>

    Really appreciate your quick response! Thank you!!

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

The topic ‘Querying relationship fields’ is closed to new replies.