1

When I just click search, wordpress takes me to the 404 page. I'd like to just have a search template (maybe a page) that does search. Can anyone help?

UPDATE:

I created a custom search page (link), and changed things around in my header file to reflect the custom search page, however now the search is broken. Can you check out the code and help me out?

2
  • If search function is implied but broken then there is likely issue with theme or some other code. You should figure that one before jumping to customization. Which theme are you using? Link to it if publicly available.
    – Rarst
    Commented Sep 1, 2011 at 20:58
  • I've customly created the theme at link and I have search.php and no searchform, because the form is hardcoded into the theme (header.php) Does WordPress only use search.php to query posts? What are they technically displayed on?
    – Josh Allen
    Commented Sep 1, 2011 at 23:26

3 Answers 3

1

I feel bad for answering my own question on here, but here's what I did.

I created a custom search template, a custom searchform.php and changed my header.php to reflect my custom search page.

What I did is rename the search box names to search instead of s to get around WordPress automatically running search.php and coming up with a 404 error (still not sure why it happened, probably my fault in search.php) and then used a new WP_Query while setting my arguments. While my solution does not provide anything more than a search term, it could be easily implemented to pull other key-value pairs into the arguments array.

searchform.php

<div class="search">
    <form method="get" class="search-form" id="search-form" action="<?php bloginfo( 'url' ); ?>/search/">
    <div>
        <input class="search-text" type="text" name="search" id="search-text" value="Search this site" />
        <input class="search-submit" type="submit" name="submit" id="search-submit" value="Search" />
    </div>
    </form>
</div>

search-template.php snippet

$s = wp_specialchars(stripslashes($_GET["search"]), 1);
$search_query = array(
    's' => $s
);

$search = new WP_Query($search_query);

So essentially s is now search to get around WordPress automatically using search.php.

If anyone has any questions, feel free to post a comment.

1
  • 1
    This helped me solve the same problem (nearly 6 years later!). Changing that WordPress default 's' to 'search' made the difference--finally I have a nice custom search page and no more 404s.
    – Tamara
    Commented Jul 13, 2017 at 8:04
0

Check to see that you have a search.php (results page) and searchform.php(code of the search box). could be something wrong with your searchform.php file.

0

You are including some extra arguments in search query. So when you have no text in query, but do have those parameters WordPress gets confused.

Template for search results display is selected according to template hierarchy:

  • search.php
  • index.php

But since you want something a little more complex then generic text search, you might need to create custom search page instead.

2
  • Then is there any way to include an if that check if the search string is emty and shows custom text? I'd like to include a search form on the page and some descriptive text rather than 'Not Found'.
    – Josh Allen
    Commented Sep 2, 2011 at 11:03
  • @Josh Allen I am not completely sure, but the issue seems to be that empty search is not being recognized as search at all. And if it is not recognized, then it won't pick up search.php template that would be a place for such check. As per answer I'd look into creating completely custom search page if complexity is anything but basic.
    – Rarst
    Commented Sep 2, 2011 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.