• How do you alter search results? My theme wants it paginated. I would really prefer it all came in as a list

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can add a filter to your functions.php file that sets parameters before posts are requested from the database. However, setting posts to -1 is generally not a good idea, especially for search for results. If you have a significant amount of posts, you could crash your site. Ideally you should set a high number (500?) and paginate from there.

    You can read more about that here:
    https://10up.github.io/Engineering-Best-Practices/php/#performance

    If you’re using a free or purchased theme, make sure you create a child theme.

    function search_filter($query) {
    
         // make sure we're not on the admin and we are in the main query
         if ( !is_admin() && $query->is_main_query() ) {
             // only set the post parameter if we're searching for posts
             if ($query->is_search) {
                 $query->set('posts_per_page', -1);
             }
         }
    }
    
    add_action('pre_get_posts','search_filter');
    Thread Starter wrdslngr

    (@wrdslngr)

    Thanks. I’ll so some research.

    If you want to see what it does, go to https://www.chattahoocheetech.edu and run a search.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Altering Search Form’ is closed to new replies.