• Hi,
    I have problem with search result. It’s random following the index.php code.
    Here the frontpage (home):

    <div class="smart_content" align="center">
    <?php query_posts(array('orderby' => 'rand')); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php include TEMPLATEPATH.'/product.php'; ?>
    <?php endwhile; ?><div class="clear"></div>
    <?php { wp_pagenavi(); } ?>
          <?php else : ?>
    	<h2>Not Found</h2>
    	<div class="bodycontent">Sorry, but you are looking for something that isn't here.</div>
          <?php endif; ?>
    </div>

    The search result is random.
    I’ve input
    <?php if (is_search()) { $posts = query_posts( $query_string . '&orderby=title&order=asc' ); } ?>
    before the random query but not work.

    How I split the query for the “home” and “search result” ?

Viewing 4 replies - 1 through 4 (of 4 total)
  • I think you have to add wp_reset_query(); after the loop.

    For example:

    <?php if (have_posts()) : ?>
                   <?php while (have_posts()) : the_post(); ?>
                   <!-- do stuff ... -->
                   <?php endwhile; ?>
         <?php endif; ?>
    <?php wp_reset_query(); ?>

    This will probably solve your issue.

    Thread Starter Platinums

    (@platinums)

    <?php query_posts(array('orderby' => 'rand')); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <!-- do stuff ... -->
          <?php endif; ?>
    <?php wp_reset_query(); ?>
    <?php if (is_search()) { $posts = query_posts( $query_string . '&orderby=title&order=asc' ); } ?>

    I’ve add this, but still random result in search. Can you explain where to put `<?php query_posts(array(‘orderby’ => ‘rand’)); ?> and <?php if (is_search()) { $posts = query_posts( $query_string . ‘&orderby=title&order=asc’ ); } ?>

    Oh, I’m afraid I didn’t understand you. If you want to select at random, except for the search-page you’d have to put it like this:

    <?php 
    
    if(is_search()){
    	// search query
    	query_posts('orderby=title&order=asc');
    } else {
    	// others query
    	query_posts('orderby=rand');
    }
    
    ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    	<!-- do stuff ... -->
    
    <?php endwhile; ?>
    <?php endif; ?>
    <?php wp_reset_query(); ?>
    Thread Starter Platinums

    (@platinums)

    Ah thats it.
    Thanks man.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Set random Home but Search is random’ is closed to new replies.