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

Search by tags drop down WordPress

  • SOLVED

I have a search page that allows users to search the following:

[1. Keywords] [2. Category/Custom_Posttype dropdown] [3. Tags dropdown]

<strong>Screengrab </strong>http://grfk.co.nz/FxCm

The search works fine with the [1. Keywords] & [2. Category/Custom_Posttype], but the [3. Tags] filter seems to get ignored. I need to be able to search by any combination of the keywords & two dropdowns.


<strong>Search form code</strong>

<div class="wrap landing clearfix">
<article class="clearfix search-results">
<h1>Search Results</h1>
<form class="search-page-form" action="/">
<h4 class="meta-heading">Advanced Search</h4>
<div class="clearfix">
<fieldset>
<label>Keywords</label>
<input type="text" class="search form-text" name="s" id="s" value="<?php echo trim( get_search_query() ); ?>" placeholder="Type keywords here" />
</fieldset>
<fieldset>
<label>Category</label>
<div class="styled-select">
<div class="icon"></div>

<?php function dd_categories($exclude) {
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'hierarchical' => 1,
'exclude' => $exclude,
'include' => '',
'number' => '',
'taxonomy' => 'category',
'pad_counts' => false );

$categories = get_categories( $args );
$menu = '<select name="post_type" id="post_type" class="postform">';
$menu .= '<option value="-1">All</option>';
foreach($categories as $category)
{
if($category->name !== 'Uncategorized' && $category->name !== 'Blogroll')
{

$menu .= '<option class="level-0" value="'.$category->term_id.'">'.$category->name.'</option>';
}
}

$menu .= '<option class="level-0" value="reading">What I\'m Reading</option>';
$menu .= '<option class="level-0" value="video">Inside ACC</option>';
// $menu .= '<option class="level-0" value="event_post">Events</option>';
$menu .= '</select>';

echo $menu;

};?>

<?php dd_categories('1,16,8');?>
</div>
</fieldset>
<fieldset>
<label>Sector Filter</label>
<div class="styled-select">
<div class="icon"></div>
<? wp_dropdown_categories(array('taxonomy'=> 'post_tag','hide_empty' => 0, 'name' => 'my_tags', 'exclude' => '14,4', 'show_option_none' => 'All')); ?>
</div>
</fieldset>
</div>
<fieldset class="submit">
<input type="submit" class="submit button btn-blue" name="submit" id="searchsubmit" value="Search"/>
<a href="/search/" class="reset-link">Reset</a>
</fieldset>

</form>



<strong>Search filter</strong>

// Search filter

function SearchFilter($query) {
if ($query->is_search or $query->is_feed) {
if(is_numeric($_GET['post_type'])) {
$query->set('cat', $_GET['post_type']);
}

else{
$query->set('post_type',$_GET['post_type']);
}
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

Answers (2)

2012-08-22

Pavel Petrov answers:

in about 10 minutes :)


Nick comments:

Awesome! I can give you login details to the test site if that helps?


Pavel Petrov comments:

Cool it will be much easier


Nick comments:

Sent you a PM with the details :)


Pavel Petrov comments:

I`m pasting the answer here so that the others would see it too. Change the code in functions.php to:

function SearchFilter($query) {
$args = array();
if ($query->is_search or $query->is_feed) {
if(is_numeric($_GET['post_type'])) {
if($_GET['post_type'] !== "-1")
{
$query->set('cat', $_GET['post_type']);
}
}
else{
if($_GET['post_type'] !== "-1")
{
$query->set('post_type',$_GET['post_type']);
}
}
if(isset($_GET['my_tags']))
{
if($_GET['my_tags'] !== "-1")
{
$query->set('tag_id',$_GET['my_tags']);
}
}
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');


Pavel Petrov comments:

Without the empty $args array, forgot to remove that...

2012-08-22

Michael Caputo answers:

I've looked into something similar recently - haven't found a solution & gave up looking. I don't think it's possible.

Could a google custom search page help you out? I can set that up for you if you're interested.


Nick comments:

Yeah I looked at that, it's too slow to index unfortunately :(