customization - Custom Search Query

I'd like to set up a custom search page that does the following:User checks off several items in a form that he

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?

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?
Share Improve this question asked Apr 8, 2014 at 15:18 PeanutPeanut 611 gold badge1 silver badge2 bronze badges 6
  • 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 Commented Apr 8, 2014 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 Commented Apr 8, 2014 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 Commented Apr 8, 2014 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/Class_Reference/WP_Query#Search_Parameter – jave.web Commented Jan 20, 2017 at 12:04
  • Please go through the same question at stackoverflow stackoverflow/questions/9523881/… – Amit Kumar PRO Commented Sep 4, 2019 at 10:42
 |  Show 1 more comment

2 Answers 2

Reset to default 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!

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745002948a4605607.html

相关推荐

  • customization - Custom Search Query

    I'd like to set up a custom search page that does the following:User checks off several items in a form that he

    22小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信