Last active
December 28, 2015 10:29
-
-
Save josephhinson/7486564 to your computer and use it in GitHub Desktop.
Search Filter (to return results by post type). Create a custom search form where you can set a hidden input to search that specific post type...see below the PHP block.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function otg_optional_search_filter($query) { | |
| $post_type = $_GET['type']; | |
| if (!$post_type) { | |
| $post_type = 'any'; | |
| } | |
| if ($query->is_search) { | |
| $query->set('post_type', $post_type); | |
| }; | |
| return $query; | |
| }; | |
| add_filter('pre_get_posts','otg_optional_search_filter'); | |
| // Search form below allows you to pass a hidden input called "type" whose value will be parsed through the search function | |
| ?> | |
| <form class="form-search" method="get" action="<?php bloginfo('url'); ?>" id="searchform"> | |
| <input type="text" placeholder="search the site" class="input-medium search-query" name="s"> | |
| <input type="hidden" name="type" value="post" id="type"> | |
| <button type="submit" class="btn">Search</button> | |
| </form> | |
| <?php | |
| // You can additionally use more than one post type by setting the inputs up as arrays, seen below: | |
| ?> | |
| <form class="form-search" method="get" action="<?php bloginfo('url'); ?>" id="searchform"> | |
| <input type="text" placeholder="search the site" class="input-medium search-query" name="s"> | |
| <input type="hidden" name="type[]" value="page" id="type"> | |
| <input type="hidden" name="type[]" value="post" id="type"> | |
| <button type="submit" class="btn">Search</button> | |
| </form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment