404 or a failed search breaks custom loops (3 posts)

  1. nouveller
    Posted 6 years ago #


    I'm using this loop in header.php to pull in the latest posts thumbnails.

    	$query = new WP_Query();
    <?php if (have_posts()) : ?>
    <?php while ($query->have_posts()) : $query->the_post(); ?>
    	<a href="<?php the_permalink(); ?>" title="View <?php the_title(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
    <?php endwhile; ?>
    <?php endif; wp_reset_query(); ?>

    Which works fine, but if you make the site 404 or perform a search that doesn't bring back any results it affects the custom loop too (not displaying anything).

    Any ideas how to stop 404's and failed searches from interfering?

  2. Hax
    Posted 6 years ago #

    Try pass your arguments to the new WP_Query object like this WP_Query(array('posts_per_page' => 300 )) don't call your variable $query use something like $current_posts because your probably overwriting the global $query; also I don;t think you need to destroy the old query wp_reset_query(); because your using a new object not the default you destroy the query when your using 'query_posts('showposts=5')' because this overwrites the original query so basically your killing the original query

    Hope this helps

  3. nouveller
    Posted 6 years ago #

    Thanks for the reply but unfortunately it didn't work. I've gone back to an old snippet of code that seems to work.

    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();

Topic Closed

This topic has been closed to new replies.

About this Topic