0

I have a custom searchform.php and search.php in my theme. However, when searching for something all I see is the front page (with mysite.com/?s=something in the adressbar). I use a filter to make Wordpress use my custom search form (don't know why it won't use it otherwise). The search does work when I use a different theme.

Filter code snippet in functions.php:

function search_form_no_filters() {
  // look for local searchform template
  $search_form_template = locate_template( 'searchform.php' );
  if ( '' !== $search_form_template ) {
    // searchform.php exists, remove all filters
    remove_all_filters('get_search_form');
  }
}
add_action('pre_get_search_form', 'search_form_no_filters');

My searchform.php

<form id="searchform" action="<?php echo home_url( '/' ); ?>" method="get" role="search">
    <div class="input-group">
        <input id="s" type="text" name="s" class="form-control">
        <span class="input-group-btn">
            <button class="btn" type="button" id="submit">Go</button>
        </span>
    </div>
    <script>
        jQuery(document).ready(function($){
            $('#searchform #submit').on('click', function(){
                $('#searchform').submit();
            }); 
        });
    </script>
</form>

For debugging purposes I currently use search.php from the Twentyfourteen theme.

I've researched this problem and it seems that the most common mistake is that the search query input field has a name other than "s". I got that right though.

7
  • What have you done to debug? If you remove your searchform.php, and search using the default search form, does your Theme's search.php load properly? If you remove your custom filter, does your Theme's search.php load properly? Apr 14, 2014 at 16:36
  • It does not work even when I use the default search form and search with no filter.
    – eevaa
    Apr 14, 2014 at 17:03
  • And if you remove search.php, does the search results index load properly? Apr 14, 2014 at 17:30
  • No. But I just noticed that it does show "Results for 'something'" as the page title (at the top of the browser window) with and without search.php
    – eevaa
    Apr 14, 2014 at 18:31
  • So how do you know that the search template is not being shown? Are you testing with a valid search term? Apr 14, 2014 at 18:32

1 Answer 1

1

Why you are using this filter code, WordPress will still use searchform.php code, without it?

Further, it could be some issue with the loop code within search.php. Twentyfourteen theme looks for content-[post-format].php file. Do you have one in you theme root?

get_template_part( 'content', get_post_format() );

try using this simplified loop in search.php ...

        <?php if(have_posts()) : while(have_posts()) : the_post(); ?>

            <?php the_content(); ?>

        <?php endwhile; endif; ?>
3
  • I also tried search.php-files from other themes and I tried a highly simplified search.php that would only output the posts' titles. Nothing worked.
    – eevaa
    Apr 14, 2014 at 18:34
  • still there could be some problem in other search.php code, but not sure about it. I've updated the answer. It might help somehow. Apr 14, 2014 at 18:46
  • Still doesn't work.
    – eevaa
    Apr 14, 2014 at 18:50

Your Answer

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

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