WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to show only a random selected number of search results from a search. Its different to the random output from a category. Any Ideas how to do this? it is for a big database to give every post of the search result the chance to be viewed.

share|improve this question

This is easy to do using the pre_get_posts action hook to set the orderby to rand in the query, something like this:

function random_search_result( $q ) {
    if ( is_search() && is_main_query() )
        $q->set( 'orderby', 'rand');
add_action( 'pre_get_posts', 'random_search_result' );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.