Unable to create custom search results template page

I’m trying to create a search results page for our website and referring to the following documentation:

https://codex.wordpress.org/Creating_a_Search_Page

I created a new page template searchpage.php with the following comment block

/*
 * Template Name: Search Page
 */

Then I added a blank page in WordPress, and assigned the above search template to the page from admin.

When using my search form, I am redirected to http://mysite.dev/search/?s=search_term as expected, however, I see 1 search result which appears to be of the search page itself.

When trying to access the search query, with get_search_query() I get nothing back even though it has been set in the URL.

I added the snippet as per the above documentation page to my template page:

https://codex.wordpress.org/Creating_a_Search_Page#Preserving_Search_Page_Results_and_Pagination

<?php
global $query_string;

$query_args = explode("&", $query_string);
$search_query = array();

if( strlen($query_string) > 0 ) {
    foreach($query_args as $key => $string) {
        $query_split = explode("=", $string);
        $search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach
} //if

$search = new WP_Query($search_query);
?>

The only query parameter that is apparently set, is pagename=search, the s parameter is nowhere in sight?

I’ve updated to WordPress 4.6, enabled the twentyfifteen theme, turned off all plugins, and seem to be getting the same behaviour on other themes as well?

Other observations I made is when I use change my search form to redirect to /, then I do get search results, but when I click on page 2 in pagination, it directs to a link like /page/2?s=search_term and I just see whatever my frontpage content is, no more search results?

Any idea what could be the issue?

0

Leave a Comment