Home » Blog Posts » Solutions » WordPress » WordPress Solution – Always reset “Custom Queries” after use

Company News:

Recent Testimonials:

WordPress Solution – Always reset “Custom Queries” after use

I’ve said it in the title of this post….and I’ll say it again, and again, and again:

“ALWAYS, ALWAYS, ALWAYS, “reset query” on completion of any and all of your WordPress custom queries, in order to avoid problems in other areas”.

I’ve learned this the hard way, and I hope this post will help make it easy for you to resolve any errors/anomalies you might be experiencing with your WordPress blog/website, as a result of using custom queries, but not resetting each one after use.

The problem I encountered was not in the display (or lack thereof) of page and/or post content (being pulled/shown by either the main loop, or the custom query loops) – those worked just fine – but rather, the problem came up when I tried to use breadcrumb-related code in my functions.php file.

The breadcrumb display would either be very erratic, or would not change at all…i.e. no matter which page I selected, it would always show the same link.

I spent many days and nights (scratching my head, and even tearing out my hair in despair) trying to find a reason/solution for this problem, until it struck me today, that perhaps it might be my custom queries which are “playing up”. And was I ever right?

The solution:

Very simple!

After the “endif/endwhile” of your custom query, and depending on how your code is laid out, use either wp_reset_query();, or php wp_reset_query();.

To give you an example:

The code for one of my custom queries was as follows. Notice, how the query is not being reset (and this is what was causing the erratic behaviour in my breadcrumb code).

<?php
$aboutUsPosts = new WP_Query();
$aboutUsPosts->query('showposts=1&cat=5');
while ($aboutUsPosts->have_posts()) : $aboutUsPosts->the_post(); ?>
    <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
    <div class="storycontent">
        <!-- <?php the_excerpt(); ?> -->
        <?php the_content('Read more...'); ?>
    </div>
<?php endwhile; ?>

So all I did was to add the very last line of code (shown below), and voila! the problem just vanished, and my breadcrumbs were showing (and working) perfectly.

<?php
$aboutUsPosts = new WP_Query();
$aboutUsPosts->query('showposts=1&cat=5');
while ($aboutUsPosts->have_posts()) : $aboutUsPosts->the_post(); ?>
    <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
    <div class="storycontent">
        <!-- <?php the_excerpt(); ?> -->
        <?php the_content('Read more...'); ?>
    </div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

Hope this helps some, and do let us know if there’s something we should add/change/delete, in order to make this post more helpful to others.

Have you experienced something similar (or perhaps different, yet strange or mysterious) happen to your blog/website (as a result of custom queries which were not reset)?

Post your thoughts, comments, suggestions etc. to help others realize the dangers of not resetting custom queries.

Good luck!

And oh! Did I already say this? “ALWAYS, ALWAYS, ALWAYS, “reset query” on completion of any and all of your WordPress custom queries, in order to avoid problems in other areas”.

  • Let us help you learn: