WordPress Reset The Post Data

If you are developing a WordPress theme then you would understand all about the WordPress loop. This is the code that is used to display the posts on the correct screen that you are currently on.

The loop works by querying the database to get all the posts that it needs for the page and will loop through them all so that we can display them. Inside the loop you will have access to the post object where you can now use functions like the_title(), the_permalink(), the_content() to display all the information on the screen of the website.

This in it's simplest form works really well, but if you want to search for additional information on the post by using another WP_Query object then this will override the existing loop and replace it with the new data.

This defines the common problem in WordPress of the loop inside of a loop.

Below is an example of the problem, it starts off with the standard single.php loop, but after the content we are going to get all the posts from this author. This means that we have to make another query to the database to return all the posts from this author.

<?php
if ( have_posts() ) : ?>

	<?php while ( have_posts() ) : the_post();	?>
		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
			<header class="entry-header">
				<h1 class="entry-title"><?php the_title(); ?></h1>
			</header>

<?php
				$query = new WP_Query( 'author=' . the_author_meta('ID') );

				if($query->have_posts())
				{
					 while ( $query->have_posts() ) : $query->the_post();
					 	?><p><a href="<?php echo the_permalink(); ?>" /><?php the_title(); ?></a></p><?php
					 endwhile;
				}

			?>

			<div class="entry-content">
				<?php the_content( ); ?>
			</div>

			
		</article>
	<?php endwhile; ?>

<?php endif; ?>

As you can see from the code that we have two have_post() while loops, if you were to run this code you will no longer have the correct content printed from the_content() function. This will no longer display the post from the first while loop but will be replaced by the content of the last post from the second while loop.

This is a problem because inside the loop WordPress will set the current post it is looping through as the variable $post, if it has another loop then this variable will be overridden by the second query. Therefore anything after the second loop will not be the post you expect but will be the last post of the second loop.

Wordpress understood that this is a problem and built a function to fix this problem, this function is wp_reset_postdata();, which will reset the $post variable to the last previous query.

All we have to do is place this after the second while loop and it will fix the problem of displaying the wrong content.

<?php
if ( have_posts() ) : ?>

	<?php while ( have_posts() ) : the_post();	?>
		<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
			<header class="entry-header">
				<h1 class="entry-title"><?php the_title(); ?></h1>
			</header>

			<?php
				$query = new WP_Query( 'author=' . the_author_meta('ID') );

				if($query->have_posts())
				{
					 while ( $query->have_posts() ) : $query->the_post();
					 	?><p><a href="<?php echo the_permalink(); ?>" /><?php the_title(); ?></a></p><?php
					 endwhile;
				}
				wp_reset_postdata();
			?>

			<div class="entry-content">
				<?php the_content( ); ?>
			</div>


		</article>
	<?php endwhile; ?>

<?php endif; ?>

Advertise here

Comment