• Resolved janhamer

    (@janhamer)


    is there a way to get a random order for search results just in the case no search term was entered ?
    for all other cases results should be listed by relevance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Mikko Saari

    (@msaari)

    Add this to your theme functions.php:

    add_action( 'pre_get_posts', function( $query ) {
        if ( $query->is_search() && empty( $query->query_vars['s'] ) ) {
            $query->set( 'orderby', 'rand' );
        }
    } );

    That’ll get you random results if the query is a search and no search term is entered. Note that if there are several pages of results, each page will be independently randomized.

    Thread Starter janhamer

    (@janhamer)

    thanks !

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘random order of results when no seach term was entered’ is closed to new replies.