Search results listed randomly
-
Hello,
Does anyone knows how to make wordpress list search results randomly?
Regards
-
Haven’t tested this, but it should work. Open your theme’s search.php or index.php (if there is no search.php), find the
if(have_posts()part and place the following above it:<?php if(is_search()) { query_posts('orderby=rand'); } // continue with the rest of the template... if( have_posts() ) : while ( have_posts() ) : the_post(); ?>If there is a search.php then you don’t need the
if(is_search()) {and}parts, but it won’t hurtHello jkovis,
Thank you for your reply, unfortunately I could not answer earlier.
I have tried your code, but as I found it does not actually what I need, it displays search results in a random order ignoring the search term, e.g. user search for “little dog” and the result is a list of post displayed by random order regardless of whether it contains the term “little dog” or not.
I found that shuffle() would do what I need 🙂 but currently trying to implement it… basically what I want is when user search e.g. “little dog” and there is 10 posts containing that term, these 10 post are displayed in shuffle mode.
Regards,
Here is the proper code, based off of jkovis:
<?php if(is_search()) { query_posts( $query_string . 'orderby=rand'); } // continue with the rest of the template... if( have_posts() ) : while ( have_posts() ) : the_post(); ?>It’s the same thing, accept you append the current
$query_stringto the orderby, so you keep the search term intact.Spancer,
Unfortunately it does not work or I haven’t done as i should 🙂 it does not return any result…
Here is the index.php code before editing:
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class=”ysr”>
Your Search Results
</div>Then after I change to above suggestion:
<?php get_header(); ?>
<?php if(is_search()) {
query_posts( $query_string . ‘orderby=rand’);
}
if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>
<div class=”ysr”>
Oh sorry, that’s an error on my part. It should be this:
<?php if(is_search()) { query_posts( $query_string . '&orderby=rand'); } // continue with the rest of the template... if( have_posts() ) : while ( have_posts() ) : the_post(); ?>I forgot an
&symbol beforeorderby@spencerfinnell thanks for catching my mistake
I never really thought about how amazing is the fact how much you can achieve with the help from people you don’t know.
I tattoo on my shoulder “wordpress community rules” 🙂
One big THANK YOU for both of you guys.
The topic ‘Search results listed randomly’ is closed to new replies.