Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Search Post Type WordPress

  • SOLVED

I have been using the following snippet of code to search businesses within my Custom post Type called 'business', and it has worked well.

However I want it now to search the one search bar to search the 'business' & accommodation' Custom Post Types.

<form method="get" action="<?php echo get_option('home'); ?>/">
<div><input type="hidden" name="post_type" value="business" /><input type="text" name="s" value="<?php _e('search','directory'); ?>" class="inputbox" onBlur="if (this.value == ''){this.value = '<?php _e('search','directory'); ?>'; }" onFocus="if (this.value == '<?php _e('search','directory'); ?>') {this.value = ''; }"/></div>
</form>


I would award the prize money to the full code required for this to work.

Answers (3)

2011-06-06

Peter Michael answers:

Totally untested, just an idea:

<form method="get" action="<?php echo get_option('home'); ?>/">
<div>
<input type="hidden" name="post_type[]" value="business" />
<input type="hidden" name="post_type[]" value="accommodation" />
<input type="text" name="s" value="<?php _e('search','directory'); ?>" class="inputbox" onBlur="if (this.value == ''){this.value = '<?php _e('search','directory'); ?>'; }" onFocus="if (this.value == '<?php _e('search','directory'); ?>') {this.value = ''; }"/>
</div>
</form>


parksey18 comments:

Cheers

2011-06-06

Michael Fields answers:

This is pretty easy to do. You just need to add a new input and defining both post types as an array. The following should work.

<form method="get" action="<?php echo get_option('home'); ?>/">

<div><input type="hidden" name="post_type[]" value="business" /><input type="hidden" name="post_type[]" value="accommodation" /><input type="text" name="s" value="<?php _e('search','directory'); ?>" class="inputbox" onBlur="if (this.value == ''){this.value = '<?php _e('search','directory'); ?>'; }" onFocus="if (this.value == '<?php _e('search','directory'); ?>') {this.value = ''; }"/></div>

</form>

2011-06-06

Erez S answers:

<form method="get" action="<?php echo get_option('home'); ?>/">
<div><input type="hidden" name="post_type" value="business,accommodation" /><input type="text" name="s" value="<?php _e('search','directory'); ?>" class="inputbox" onBlur="if (this.value == ''){this.value = '<?php _e('search','directory'); ?>'; }" onFocus="if (this.value == '<?php _e('search','directory'); ?>') {this.value = ''; }"/></div>
</form>


Erez S comments:

Or this(add to functions.php):
function mySearchPostsFilter($query)
{
if ($query->is_search)
{
$query->set('cat',array('post_type' => array('business','accommodation'))); // your category IDs
}
return $query;
}
add_filter('pre_get_posts','mySearchPostsFilter');


<form method="get" action="<?php echo get_option('home'); ?>/">
<div><input type="text" name="s" value="<?php _e('search','directory'); ?>" class="inputbox" onBlur="if (this.value == ''){this.value = '<?php _e('search','directory'); ?>'; }" onFocus="if (this.value == '<?php _e('search','directory'); ?>') {this.value = ''; }"/></div>
</form>