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"
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) 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!
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
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 Commented Apr 8, 2014 at 15:47$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 Commented Apr 8, 2014 at 15:47s
parameter, don't forget to callwp_reset_postdata
after you are finished with this query :-) REF: codex.wordpress/Class_Reference/WP_Query#Search_Parameter – jave.web Commented Jan 20, 2017 at 12:04