Support

Account

Home Forums General Issues Trouble with resetting

Solved

Trouble with resetting

  • I’ve got a page with the regular page loop… and then I do a special WP_query to pull out data from a custom post type. And, within that custom WP_Query loop, I use the relationship loop (BTW, the link functionality in this HTML editor didn’t work here — was going to link to http://www.advancedcustomfields.com/resources/field-types/relationship/).

    I’ve mapped it out below. I’m including the wp_reset_postdata() after my relationship loop, but the fields afterwards aren’t displaying. So somethings weird. Any ideas?

    [REGULAR LOOP]
       [CUSTOM WP_QUERY LOOP]
           [RELATIONSHIP CODE]
              Regular custom fields -- working!
    
              Stuff from http://www.advancedcustomfields.com/resources/field-types/relationship/ Includes wp_reset_postdata(); 
    
              Regular custom fields -- NOT working!
       [/CUSTOM WP_QUERY LOOP]
    [/REGULAR LOOP]
  • Would it be possible to post your full-code? It’s impossible to troubleshoot your problem with the example, and it seems from your description that there may be no need for many nested loops. A more full description as to your goals would also be helpful.

    🙂

  • So i’m guessing it’s the following 2 field calls that are failing?

      <?php the_field("presentation_abstract"); ?>
                                        <p>
                                        	<a href="<?php the_field("presentation_download_file"); ?>" target="_blank" class="arrow">Download</a>
                                        </p>
                                    </section>	

    If this is the case then a work around would be simple, set those fields as variables further up the page, and then simply call the variable.

    <!-------------------MAIN PAGE LOOP------------------------->
      <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>  
    	
            <div id="main" class="clearfix">
    	               
    	        <?php if(trim(get_field("headline_override")) != ""): ?>
    	        	<h1><?php the_field("headline_override"); ?></h1>
    				<?php else: ?>
    	        	<h1><?php the_title(); ?></h1>
    	        <?php endif; ?>                                    
    	        <article>
    			<?php the_content(); ?>   
    	           	<div id="presentations"> 
                       	
                       	
    <!-------------------CUSTOM WP_QUERY LOOP------------------------->                   	
                       	
    				<?php
                        
    	                        // The Query
    	                        $args = array(
    	                            'post_type'	=> 'presentations',
    	                            'posts_per_page'     => -1,
    	                            'order' => 'DESC',
    	                            'orderby' => 'presentation_date',						
    	                            'post_status' => 'publish',
    					'meta_key' => 'presentation_date',
    					'meta_query' => array(
    						array(
    						'key' => 'archived_presentation',
    						'value' => 'true',
    						'compare' => '!='
    					)
    				)							
    	                        );						
                            $custom_loop = new WP_Query($args);			
                            #echo $custom_loop->request; 	
    						
    			$previous_year = "";						
                            
                            // The Loop
                            if( $custom_loop->have_posts() ): ?>
                                <?php while( $custom_loop->have_posts() ): $custom_loop->the_post(); ?>
                                	<?php 
    					$date = DateTime::createFromFormat('Ymd', get_field('presentation_date'));
    					$current_year = $date->format('Y');		
    					
    					if($previous_year != $current_year) { echo "<p>" . $current_year . "</p>";}
    					$previous_year = $current_year;
    				?>
                                    <section>
                                    <h2><a href="<?php the_field("presentation_download_file"); ?>" target="_blank"><?php the_title(); ?></a></h2>
                                       
    <?php $presentationDownloadFile = get_field("presentation_download_file");
    $presentationAbstract = get_field("presentation_abstract"); ?>
                                    <p>
    <!-------------------FIRST ACF RELATIONSHIP LOOP THAT BREAKS THINGS------------------------->                                  
    					<?php
    					#--------- research center
    					$posts = get_field('research_center');                         
    					if( $posts ): 
    						foreach( $posts as $post): // variable must be called $post (IMPORTANT)
    							setup_postdata($post); ?>
    							<strong>Lead Research Center:</strong> <a href="/about/#center-<?php echo $post->ID; ?>"><?php the_title(); ?></a><br />
    						<?php endforeach; 
    						wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly 
    					endif; 						
    					
    					#--------- presenters											
                                            $posts = get_field('presenters');                         
                                            if( $posts ): ?>
                                                <strong>Presenter(s):</strong>
    						<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT)
    	                                               setup_postdata($post); ?>
    	                                                <a href="/about/staff/#staff-<?php echo $post->ID; ?>"><?php the_title(); ?></a>
    	                                        <?php endforeach; 
                                                wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly 
                                            endif; 	                                    										
    										?>                                    
                                        </p>
                                        <?php echo $presentationAbstract; ?>
                                        <p>
                                        	<a href="<?php echo $presentationDownloadFile; ?>" target="_blank" class="arrow">Download</a>
                                        </p>
                                    </section>			                                                                                                     
                                <?php endwhile; ?>                           
                            <?php endif;
                            
                            // Reset Query
                            wp_reset_postdata(); ?>
                                                                                              		                     
    			</div><!-- / #presentations -->
                                                           
                	</article><!-- article -->
                </div> 
            </div>                                              
        <?php endwhile; ?>            

    p.s. I don’t really get what you are trying to do so I can’t comment on whether the nested loops are required or not, but I think for single pages your first loop shouldn’t be required. I might be wrong though 😉

  • It actually fails after the first relationship field (“research centers”) so then the second relationship field (“presenters”) also doesn’t display.

    I had considered doing what you suggest… but I figured it would be better if I could just reset things properly. 🙂

    Thank you for looking… if you have any other ideas, let me know.

  • Ah-ah, I found a workaround. I just noticed now that the ACF relationship docs have a second option that doesn’t set postdata: http://www.advancedcustomfields.com/resources/field-types/relationship/

    /*
    *  Loop through post objects ( don't setup postdata )
    *  Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question.
    */
     
    $posts = get_field('relationship');
     
    if( $posts ): ?>
    	<ul>
    	<?php foreach( $posts as $post_object): ?>
    	    <li>
    	    	<a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>
    	    	<span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span>
    	    </li>
    	<?php endforeach; ?>
    	</ul>
    <?php endif;

    This helps me avoid the situation!

  • Right.

    I think the problem is that you’re resetting data for everything, so once that first relationship first is closed and the data is reset, the data from the parent loops gets reset also (possibly).

    Seeing as you are not using any ACF functions or anything too specific, just change the relationship codes to: 1) remove the setup_postdata, 2) use $post->post_title instead of the_title() and 3) remove the resets.

    EDIT: beat me to it

  • Yep, it’s working!

  • using @relish27 suggestion
    <?php the_field('field_name', $post_object->ID); ?>
    instead of
    wp_reset_post_data();
    saved my life.

    I was not able to get
    wp_reset_post_data(); to without a main loop, just with custom queries.

  • I just want to say THANK YOU for this post. I found it relatively quickly in my search and this saved me a ton of time.

    I am using relationships to link content from other pages, inside the relationships is other post data, so resetting was causing everything below the reset to break. This was a life saver!

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

The topic ‘Trouble with resetting’ is closed to new replies.