Display search results within the same page

The simplest option if you want to show search results within a page context you’ll need to do a custom loop, otherwise you won’t be able to access the page information.

Change the input with the name s to something else like search or q to stop wordpress from doing it’s usual built in search.

Next change the form action parameter to the current page’s URL. You can use <?php get_permalink(); ?> for that.

The loop you need to do is as follows:

<?php
    if ( isset( $_REQUEST[ 'search' ] ) ) {
          // run search query
          query_posts( array(
             's' => $_REQUEST[ 'search' ],
             'post_type' => $_REQUEST[ 'post_type' ],
             'paged' => $paged
             )
          );

        // loop
        if ( have_posts() ) : while ( have_posts() ) :
            // loop through results here
        endwhile; endif;

        // return to original query
        wp_reset_query();
    }
?>

Leave a Comment