Support » Fixing WordPress » 404 or a failed search breaks custom loops

  • nouveller

    (@nouveller)


    Hi,

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

    <?php
    
    	$query = new WP_Query();
    
    	$query->query('posts_per_page=30');
    
    ?>
    
    <?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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hax

    (@hax)

    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

    nouveller

    (@nouveller)

    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();
    $wp_query->query('posts_per_page=30');
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘404 or a failed search breaks custom loops’ is closed to new replies.