6

I'd like to set up a custom search page that does the following:

User checks off several items in a form that he'd like to see returned in his search (essentially choosing from a list of tags).

Results are returned that match all of the tags he chose (using AND not OR).

A specific example would be:

Return all posts in the category of "Area" where tags = "elementary school" AND "park"

  1. Do I need to name my search form anything in particular?
  2. On the search results page, how do I code the custom query so that it grabs all posts within the category of Area, and that have tags of all the tags the user chose in the search form?
  • Lots of confusion in what I've read. I've seen that I should use something like query_posts('cat=32&tag=hs1+hs1&showposts=5'); and I've seen that I should use something like <?php $the_query = new WP_Query( 'cat=Neighborhood&tag=elementary school+park' ); // The Loop while ( $the_query->have_posts() ) : $the_query->the_post(); the_title(); the_content(); endwhile; wp_reset_postdata(); ?> – Peanut Apr 8 '14 at 15:47
  • and I've seen I should use something like $query = array ( 'paged' => 1, 'posts_per_page' => '5', 'offset' => 0, 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'post_type' => array ( 'post' => 'post', ), 'cat' => '35', 'tag__and' => array ( 0 => 36, 1 => 39, ), ); ... basically i'm just extremely confused. – Peanut Apr 8 '14 at 15:47
  • @Peanut Two notes: If you got additional information, please file edits instead of comments. Not everyone reads comments and those get cleaned up from time to time. Second, please use formatting (backticks) for code in comments. You got a "help" link right next to the comment form here. Thanks. – kaiser Apr 8 '14 at 15:58
  • Use WP_Query and it's s parameter, don't forget to call wp_reset_postdata after you are finished with this query :-) REF: codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter – jave.web Jan 20 '17 at 12:04
  • Please go through the same question at stackoverflow stackoverflow.com/questions/9523881/… – Amit Kumar Sep 4 at 10:42
11

1) You can use the template search.php and searchform.php as your starting points. Creating a Search Page Codex

2) As far as the custom query goes, you can use pre_get_posts hook to test if you're on a search page, then you get $_GET your values, edit your query accordingly. Action Reference - pre_get_posts

There are tons of tutorials online and questions on this exchange to help you out. Some are Simple and others are more Complex. You'll have to do some real research to accomplish this. Hope it helps!

1

In order to make a custom search you are going to want these inputs in your html. You can use the name and value attributes to pass to your URL.

<input type="hidden" class="category" name="category_name" value="recording">
<input type="hidden" class="type" name="post_type" value="post">
<input type="text" class="search-bar" id="global-search"><button class="search-submit" type="submit" id="search-submit">

Then in your scripts file you are going to want to build your URL.

// update search type
let type = $('input.type').val();
if(type === 0 || type == undefined){
  type = "";
}
// update search category
let category = $('input.category').val();
if(category === 0 || category == undefined){
  category = "";
}
// update search keyword
keyword = '/?s=' + $(this).val() + '&post_type=' + type + '&category_name=' + category;
$('.search-keyword').attr('data-target','/cimuk' + keyword);
$('.search-keyword .label').text('Search for ' + '"' + $(this).val() + '"');

//call this function on submit or enter
function goKeyword(){
    window.location.href = $('.search-keyword').data('target')
}

Then you can add this filter to your funcitons.php. This will grab the variables from the URL and pass them to the search query.

//Search Post type
function mySearchFilter($query) {
    $post_type = $_GET['post_type'];
    $category = $_GET['category_name'];
    if (!$post_type) {
        $post_type = 'any';
    }
    if (!$category){
        $category = null;
    }
    if ($query->is_search) {
        $query->set('post_type', $post_type);
        $query->set('category_name', $category);
    };
    return $query;
};


add_filter('pre_get_posts','mySearchFilter');

The results should load on your search.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.