Support

Account

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

Solving

Relationship field does not allow for post ID to be reset, help.

  • This is kinda driving me a little crazy. I’m wordpress 4.1, Advanced Customer Fields v4 and setting up a Relationship custom field. The field is set to output the post object. I copied the example code from the Advanced Custom Fields website. I’ve tried both options.

    This one:

    $posts = get_field('relationship_field_name');
    
    if( $posts ): ?>
        <ul>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <span>Custom field from $post: <?php the_field('author'); ?></span>
            </li>
        <?php endforeach; ?>
        </ul>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>

    And this one:

    $posts = get_field('relationship_field_name');
    
    if( $posts ): ?>
        <ul>
        <?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
            <li>
                <a href="<?php echo get_permalink( $p->ID ); ?>"><?php echo get_the_title( $p->ID ); ?></a>
                <span>Custom field from $post: <?php the_field('author', $p->ID); ?></span>
            </li>
        <?php endforeach; ?>
        </ul>
    <?php endif; ?>

    I have this code before the standard wordpress page loop. However, it seems that this code doesnt want to play nice with the loop. It keeps picking up the post ID of the item that is chosen in the Relationship field.

    Here is my code:

    <?php $posts = get_field('sidebar_promo_short_left');
    
    if( $posts ): ?>
        <?php foreach( $posts as $p): ?>
            <?php 
                        $attachment_id = get_field('promo_image', $p->ID);
                        $size = "promo-sidebar";
                        $image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
    
                    if( $image_attributes ) {
                    ?>
                <a href="<?php echo the_field('promo_url', $p->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;
    endif; 
    ?>

    And then later in my page template i have this:

    <?php while ( have_posts() ) : the_post(); ?>
    
      <?php echo the_ID(); ?>
    
    <?php endwhile; ?>

    That ID is outputting the ID of the object in the relationship field, not the ID of the page ID. What’s going wrong here? I have wp_reset_postdata() but it doesnt seem to want to reset the postdata.

    ======

    I was able to make some progress. I put this code inside of a standard wordpress loop and the fields would load. But somewhere along the way something is messing with the $post->ID. I just want a sure fire way to reset that.

    I’m going to try to wrap each of the Relationship fields inside of a custom wp-query to see if that helps at all.

    Do you know what would cause the $post->ID to not reset eventhough i have wp_reset_postdate() at the end of all of my foreach statements?

  • I found a work around, i suppose, but if you can tell me if this is the way i am supposed to use this field type, that would be helpful.

    So to solve my problem, i wrapped every Relationship field query in a custom WP_QUERY loop. I pull the page/post id to load the current pages fields.

    I’m thinking its better to use wp_query and not the wordpress loop. Doing it this way does seem to make it not ruin $post->ID for other sections of the page.

    Here is a sample code from my page…i do the same thing in the header, page.php and sidebar.php – I’ve setup section on each page that will load a post that is assigned to it (I’m using to show banner ads in different spots of the page based on the settings the user makes. They can choose the banner ad per page).

    <?php 
    $postid = get_the_ID();
    
    $homepgqry = array( 
    	'p' => $postid,
    	'post_type' => 'any',
    	'posts_per_page' => 1,
    	'status' => 'published'
    );
    
    $loop = new WP_Query( $homepgqry );
    while ( $loop->have_posts() ) : $loop->the_post();
     
    $posts = get_field('homepage_promo_bottom');
    
    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php //setup_postdata($post); ?>
            <?php 
    					$attachment_id = get_field('promo_image');
    					$size = "promo-header";
    					$image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
    				
    				if( $image_attributes ) {
    				?>
            	<a href="<?php echo the_field('promo_url'); ?>" 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 // wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    
    <?php endwhile; wp_reset_query(); ?>
  • Actually that did not solve the problem. It seems anytime i put a relationship field in the header, page, sidebar, anywhere – anytime there is a wordpress default loop after that, that loop won’t run.

    If i comment out this code in my header-news.php file, then the rest of the page will run just fine.

    I have tried variations of this code too, but it doesnt seem to work.

    Maybe if you can take a look you can spot something wrong with it.

    <?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();
    
    $posts = get_field('header_promo_banner');
    
    if( $posts ): ?>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <?php 
    					$attachment_id = get_field('promo_image');
    					$size = "promo-header";
    					$image_attributes = wp_get_attachment_image_src( $attachment_id, $size );
    				
    				if( $image_attributes ) {
    				?>
            	<a href="<?php echo the_field('promo_url'); ?>" 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 wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    <?php endwhile; endif; wp_reset_query(); ?>

    On most pages i am using global $post; $post_id = $post->ID; to get the ID of the current page outside of the loop.

    I was hoping that wp_reset_query would reset the loop and let the items after this query run, but it doesnt seem to work.

    Then the code in my single-10.php file is pretty basic:

    while ( have_posts() ) : the_post(); ?>
    
    	<div class="main-photo-wrapper">
      	<img src="<?php echo the_field('header_photo'); ?>" style="width:100%; height:auto;" alt="<?php echo the_field('header_alt_text'); ?>"/>
    	</div>
      
      <div class="title-bar">
      	<h2><?php the_title(); ?></h2>
      </div>
      
      <div class="article-content">
      <?php the_content(); ?>
      </div>
      
    <?php endwhile;  // end of the loop. ?>
  • 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?

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

The topic ‘Relationship field does not allow for post ID to be reset, help.’ is closed to new replies.