Support

Account

Home Forums Backend Issues (wp-admin) Post Object not resetting with wp_reset_postdata()

Solved

Post Object not resetting with wp_reset_postdata()

  • Sorry, I have tried to mess around with this in a bunch of different ways but just can’t get it to work. I’m sure it’s something so simple that I am overlooking. The problem is that in this page template I am using the get_field() function to retrieve a post object. Once I do what I need to do with the post’s data, I try to reset the post object to the current post. As far as I know, I am following the instructions as found on the website, but it is not resetting the post data. Here is the code:

    
    <!--Headline-->
    <div id="headline" class="home">
    	<?php 	$post_object = get_field( 'home_testimonial', 90 ); //Retrieves the Testimonial Page content
    			if( $post_object ):
    				$post = $post_object;
    				setup_postdata( $post );
    	?>	
    	<div id="headline-content">
    		<h2>What Our Clients are Saying</h2>
    		<p>"<?php the_field( 'blurb' ); ?>"&nbsp;&nbsp;&nbsp;&nbsp; -
    			<span class="customer-name"> <?php the_field('name'); ?></span>
    			<span class="customer-location"><?php the_field('location'); ?></span>
    		</p>
    	</div>
    	<a href="<?php echo home_url( '/testimonials.php' ); ?>" class="headline-readmore"></a>
    		<?php wp_reset_postdata(); //why is this not working??? ?>
    	<?php	endif; ?>
    </div> 
    <!-- /headline -->
    
    <!-- Page -->
    <div id="page">
    	<div id="main-content" class="home" >
    		<h2><?php the_title(); the_field( 'home_headline' ); //should be getting data from current post, but it's not ?></h2>
    		
    	</div><!-- /main-content -->
    

    When I execute print_r( $post ) after wp_reset_postdata(), it is still giving me values from the old post data. Again, I’m betting it’s an easy fix. Hopefully I’m right

  • Hi @truheart

    Can you confirm that the template above is loaded for a $post?
    Is this template a page.php, page template, or single.php?

    The fact that you use a post_id param of 90 to load the post value, makes me think that there is no global $post object to start with…

    Thanks
    E

  • Thanks Elliot, I knew it was simple. I forgot to make it a page template. Everythings working smooth now. Thanks again.

  • I just ran into the same issue as truheart. However, I utilized WP_Query in my loop to call my custom post type. My code is similar to:

    $args = array( 'post_type' => 'customposttype' );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();

    In this case, I found the way to reset the post data was to run:
    $loop->reset_postdata(); instead of wp_reset_postdata(); (due to the fact I have no global $post object defined).

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

The topic ‘Post Object not resetting with wp_reset_postdata()’ is closed to new replies.