Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.