Tagged: , ,

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #384369

    Dear Kriesi,
    Is it possible to see the search results from posts in a random order?

    Thanks in advance,

    Pieter

    #385153

    Hi pieterpieterpieter!

    You can add this to the bottom of your functions.php file.

    add_filter( 'pre_get_posts', 'enfold_customization_random_search' );
    function enfold_customization_random_search( $q ) { 
    	if ( $q->is_search ) { $q->set( 'orderby', 'rand' ); }
    	return $q;
    }

    Though I’m not sure why you would want to do that. It defeats the purpose of searching.

    Cheers!
    Elliott

    #387144

    Hmm it doesn’t work.

    I have already managed to show my search results in a grid.

    Now i just need to make it random. Why? Because it’s a restaurant listing website. I want to show them random so it’s not always the same restaurant first you see?

    Thanks!

    Pieter

    #387186

    Thank God i found a solution!

    For anyone with this problem:

    Add this to your functions.php file.

    function random_search_result( $q ) {
    if ( is_search() && is_main_query() )
    $q->set( ‘orderby’, ‘rand’);
    }
    add_action( ‘pre_get_posts’, ‘random_search_result’ );

    That’s it!

    Thanks for the support Kriesi! Fantastic like always!

    Pieter

    #387244

    Hey!

    Glad you found a solution. Thank you for this update.

    Enjoy the theme.

    Best regards,
    Günter

Viewing 5 posts - 1 through 5 (of 5 total)
  • The topic ‘Search results: random order’ is closed to new replies.