Multiple Category or Tag Search
-
Hi People,
I’m looking for a way to create a search feature which users can search KEYWORDS and select between multiple category’s.
I looked at WP Custom Search and it does work but does not work how I want it too. When I search for results it uses my Themes index.php which has a main image slider.
Anyways I have been trying to create a form in which I can use keywords a category and then tags. But just don’t have the knowledge to complete it.
I have this in a 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( 'child_of=4' ); ?> <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>
This then uses the 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:/am2pm/?s=$text&cat=$cat&tag=$tag"); ?>
The code takes what you have entered and changes the URL.
For my search page I just use the normal <?php get_search_form(); ?> which directs to the first search form.
This coding was found on a older post and seems to work, just the keywords do not work.
This is the closest I have got to what I want and WP Custom Search.
Im looking to have:
KEYWORDS ….
Location ….
Job Type ….
If anyone has any help or idea then I would be grateful.£
- The topic ‘Multiple Category or Tag Search’ is closed to new replies.