Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I built a custom search with the following code:

functions.php

function template_chooser($template)   
{    
  global $wp_query;   
  $post_type = get_query_var('post_type');   
  if( $wp_query->is_search && $post_type == 'products' )   
  {
    return locate_template('archive-search.php');  //  redirect to archive-search.php
  }   
  return $template;   
}
add_filter('template_include', 'template_chooser'); 

search-archive.php

<?php
        /* Template Name: Custom Search */        
        get_header(); ?>             
        <div class="contentarea">
            <div id="content" class="content_right">  
                     <h3>Search Result for : <?php echo "$s"; ?> </h3>       
                     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>    
                <div id="post-<?php the_ID(); ?>" class="posts">        
                     <article>        
                    <h4><a href="<?php the_permalink(); title="<?php the_title();     ?>"><?php the_title(); ?></a><h4>        
                    <p><?php the_exerpt(); ?></p>        
                    <p align="right"><a href="<?php the_permalink(); ?>">Read     More</a></p>    
                    <span class="post-meta"> Post By <?php the_author(); ?>    
                     | Date : <?php echo date('j F Y'); ?></span>    

                    </article><!-- #post -->    
                </div>    

           </div><!-- content -->    
        </div><!-- contentarea -->   
        <?php get_sidebar(); ?>
        <?php get_footer(); ?>

search form

  <div>   
     <h3>Search Products</h3>
     <form role="search" action="<?php echo site_url('/'); ?>" method="get" id="searchform">
       <input type="text" name="s" placeholder="Search Products"/>
       <input type="hidden" name="post_type" value="products" /> 
       <input type="submit" alt="Search" value="Search" />   
      </form>  
  </div>

The problem is with that is when I submit an empty search form it shows all posts, but not with the search-archive template. How can I fix that?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

This is not related only to your custom post_types search. WordPress has a problem on empty search on itself. I use this simple code snippet as workaround:

add_filter( 'request', 'my_request_filter' );
function my_request_filter( $query_vars ) {
    if( isset( $_GET['s'] ) && empty( $_GET['s'] ) ) {
        $query_vars['s'] = " ";
    }
    return $query_vars;
}

You may tak advantage of that and build your own code above that.

share|improve this answer

In functions.php you're looking for archive-search.php, but your file name is search-archive.php. Is that a simple typo, or is that your problem?

share|improve this answer
    
it was a simple typo, but thanks for the attention –  robert Mar 14 '13 at 20:25
    
NP. I've chased bugs for hours, only to find out I'd misspelled a variable or something. –  Pat J Mar 14 '13 at 20:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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