DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. WordPress: Reset The Post Data

WordPress: Reset The Post Data

Paul Underwood user avatar by
Paul Underwood
·
Mar. 07, 13 · Interview
Like (0)
Save
Tweet
Share
3.06K Views

Join the DZone community and get the full member experience.

Join For Free

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();
					 	?>
<a href="<?php echo the_permalink(); ?>" /><?php the_title(); ?></a>
<?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();
					 	?>
<a href="<?php echo the_permalink(); ?>" /><?php the_title(); ?></a>
<?php
					 endwhile;
				}
				wp_reset_postdata();
			?>
<div class="entry-content">
				<?php the_content( ); ?>
			</div>
</article>
	<?php endwhile; ?>
<?php endif; ?>




POST (HTTP) WordPress Data (computing) Reset (computing)

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Key Considerations When Implementing Virtual Kubernetes Clusters
  • A Complete Guide to AngularJS Testing
  • How to Develop a Portrait Retouching Function
  • How To Use Terraform to Provision an AWS EC2 Instance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: