Support

Account

Home Forums General Issues Relationship field does not allow for post ID to be reset, help. Reply To: Relationship field does not allow for post ID to be reset, help.

  • I looks like when we use $posts = get_field(‘relationship_field’);

    and then use foreach ($posts as $post):

    This causes issues with the $posts variable. I know i was not able to reset it.

    So, i saw this thread on your board.

    http://support.advancedcustomfields.com/forums/topic/displaying-a-relationship-field-on-a-custom-post-type-taxonomy-page/

    And i modified my code to match what her solution ended up being:

    And now i have this:

    <?php
    $post_id = 19;
    
    $headerqry = array( 
    	'p' => $post_id,
    	'post_type' => 'any',
    	'posts_per_page' => 1,
    	'status' => 'published'
    );
    
    $loop = new WP_Query( $headerqry );
    
    if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
    
    $promo_posts = (array) get_field('header_promo_banner');
    
    if( $promo_posts ): ?>
        <?php foreach( $promo_posts as $promo_post): // variable must be called $post (IMPORTANT) ?>
            <?php 
    					$attachment_id = get_field('promo_image', $promo_post->ID);
    					$size = "promo-header";
    					$image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
    				
    				if( $image_attributes ) {
    				?>
            	<a href="<?php echo the_field('promo_url', $promo_post->ID); ?>" target="_blank"><img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"></a>
        <?php } endforeach; ?>
    <?php endif; ?>
    <?php endwhile; endif; wp_reset_query(); ?>

    And this is letting single.php using standard wordpress loop run. So, I would say that there is definitely a problem with the Relationship field when you use the $posts variable.

    Can you please look into this?