Multiple search results with individual queries

September 16, 2013

Thanx to : Rob Barret -> http://www.rob-barrett.com/2009/05/multiple-search-results-pages-within-one-wordpress-site/ 

The trick here is to make a hidden input field with a unique identifier. In the example I use name=”site_section” with the value=”Artist”.

<form action="/" method="get" id="main-search">
 <fieldset>
 <input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
 <input name="site_section" type="hidden" value="Artist" />
 <input type="image" id="image" alt="Search" src="<?php echo get_stylesheet_directory_uri(); ?>/images/search.png" />
 </fieldset>
</form>

In my “searchfilter” I then pick up the “site_section” value via $site_section = $_GET["site_section"]. In this example the search will show result only from post type “artist_post”;

function searchfilter($query) {
$site_section = $_GET["site_section"];

if($site_section == "Artist"){
 if ($query->is_search && !is_admin() ) {
 $query->set('post_type',array('artist_post'));
 $query->set('posts_per_page',1);
 }
 }

return $query;
}

add_filter('pre_get_posts','searchfilter');

I also use different loops depending on the “site_section”

</pre>
<?php
/**
 * The template for displaying Search Results pages.
 *
 * @package WordPress
 * @subpackage Roots & Boots
 * @since Roots & Boots 1.0
 */

get_header(); ?>

<div id="primary" class="content-area">
 <div id="content" class="site-content" role="main">
 <?php $search_query = get_search_query(); ?>
 <?php if ( have_posts() ) : ?>

<header class="page-header">
 <?php rb_page_header("Sökresultat för : " . get_search_query()); ?>
 </header>

<?php /* The loop */ ?>
 <?php
 $site_section = $_GET["site_section"];
 if($site_section == "Artist"){
 get_template_part( 'loop', 'artist' );
 }else{
 get_template_part( 'loop', 'default' );
 }
 ?>
 <?php else : ?>
 <?php get_template_part( 'content', 'none' ); ?>
 <?php endif; ?>

</div><!-- #content -->
 </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Tags: ,

Leave a Comment