Hey,
there’s a plugin called Enhanced Search Form, and supposedly it lets you search with tags and categories. I didn’t get it to work (on 3.0.1) though. See if you can get it to work.
Other than that, I did manage to make something that worked for me. No idea how good it actually is, keep in mind that I am not really a php coder. Someone could probably come up with a better solution, no doubt.
You have a search form and you can search for a keyword, tags (check mutliple ones) and a category. You can choose to search for posts containing ALL tags or ANY of the chosen tags.
So the search form POSTs your choices to this other file, which will build a search URL for you and send you to it.
So try this out, at your own risk. ??
searchform.php:
<form action="<?php bloginfo('template_url') ?>/build_search.php" method="post" accept-charset="utf-8">
<div>Search</div>
<input type="text" name="keywordsearch" value="" id="title">
<div>Category</div>
<?php wp_dropdown_categories( 'show_option_all=All Categories' ); ?>
<div>Tags</div>
<select name="and-or" id="select-and-or">
<option value="OR">Match ANY of the checkboxes (default)</option>
<option value="AND">Match ALL of the checkboxes</option>
</select>
<div>
<?php
// The following will list all tags with a checkbox next to them.
$tags = get_terms( 'post_tag' );
$checkboxes = '';
foreach($tags as $tag) :
$checkboxes .='<input type="checkbox" name="tag[]" value="'.$tag -> slug.'" id="tag-'.$tag->term_id.'" /><label for="tag-'.$tag->term_id.'">'.$tag->name.'</label>';
endforeach;
print $checkboxes;
?>
</div>
<p><input type="submit" value="Search"></p>
</form>
Now the following file, there’s a lot you probably could do to shorten it, but as I mentioned I’m not really a php coder and I just typed stuff up to make it happen for me. ??
build_search.php:
<?php
// Keyword search
if (isset($_POST['keywordsearch'])) {
$text_search = $_POST['keywordsearch'];
$text = $text_search;
}
// If there is no keyword entered
else {
$text = '';
}
// If there's a category search
if (isset($_POST["cat"])) {
$cat = $_POST["cat"];
}
// If there's a tag search
if (isset($_POST["tag"])){
$tags_array = array();
$tags_array = $_POST["tag"];
foreach ($tags_array as $key => $value)
{
if ($_POST["and-or"] == "OR") {
// tag1 OR tag2 is chosen, add a comma after the tags
$string .= $value.',';
}
elseif ($_POST["and-or"] == "AND") {
// tag1 AND tag2 is chosen, add a plus after the tags.
$string .= $value.'+';
}
}
// Remove the last symbol in the string, in this case the comma or plus that is added after each tag. We don't want it to look like "tag1+tag2+".
$tags_string = substr($string, 0, -1);
$tag = $tags_string;
}
// If there's no search for tags, set it to 0
else {
$tag = 0;
}
// build the url with the variables
$url = header("Location:/fv/?s=$text&cat=$cat&tag=$tag");
?>
then the file that presents your search results,
search.php
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div>Title</div>
<?php the_title(); ?>
<div>Excerpt</div>
<?php the_excerpt(); ?>
<div>Tags</div>
<?php the_tags('',','); ?>
<div>Category</div>
<?php the_category(); ?>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.'); ?></p>
<?php endif; ?>
Hope it works for ya, worked for me.