Hướng dẫn tạo custom search cho custom post type trong wordpress

Hướng dẫn tạo custom search cho custom post type trong wordpress

Have you ever worked on a WordPress site that has a lot of custom post types? Well as-is WordPress search is a disaster which is why many bloggers use Google custom search. Well, we were working on a site that has a good amount of resource articles, videos, books, and blog posts. Using the default WordPress search just wasn’t cutting it. Using Google Search also was not a viable option. Therefore, we decided to create an advanced search form which lets user pick which area of the site they want to search by limiting custom post types via checkboxes. Users can combine their search queries and most importantly, we made it so the checkboxes are saved along with the search query. This lets the user sees exactly what they searched for, and they can modify the parameters. In this article, we will show you how to create an advanced search form in WordPress utilizing the power of the WordPress search query and limiting the results by custom post types.

Hướng dẫn tạo custom search cho custom post type trong wordpress

First thing you need to do is open your searchform.php file or wherever your search form is stored. Then add the following fields inside the form code:

<input type="hidden" name="post_type[]" value="articles" />
<input type="hidden" name="post_type[]" value="post" />
<input type="hidden" name="post_type[]" value="videos" /> 
<input type="hidden" name="post_type[]" value="books" />  

Don’t forget to replace the value with your custom post types. The code above basically limits your search results to those post types. Well, if you noticed we pretty much added all post types available except for pages. Well, there is a good reason for doing so which we will get to later. So make sure to include ALL post types that you want to search for using the main search button. These fields are hidden, so the user doesn’t see these.

Next open your search.php file and paste the following codes above your loop content, so your users can see the options at the top.

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" <?php if(is_search()) { ?>value="<?php the_search_query(); ?>" <?php } else { ?>value="Enter keywords &hellip;" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"<?php } ?> /><br />
	
<?php $query_types = get_query_var('post_type'); ?>
    
<input type="checkbox" name="post_type[]" value="articles" <?php if (in_array('articles', $query_types)) { echo 'checked="checked"'; } ?> /><label>Articles</label>
<input type="checkbox" name="post_type[]" value="post" <?php if (in_array('post', $query_types)) { echo 'checked="checked"'; } ?> /><label>Blog</label>
<input type="checkbox" name="post_type[]" value="books" <?php if (in_array('books', $query_types)) { echo 'checked="checked"'; } ?> /><label>Books</label>
<input type="checkbox" name="post_type[]" value="videos" <?php if (in_array('videos', $query_types)) { echo 'checked="checked"'; } ?> /><label>Videos</label>
    
<input type="submit" id="searchsubmit" value="Search" />
</form>

This will add a search box above your results with the search query inside the input box. This will also check which post types are being searched for in the query, and make the appropriate checkboxes checked. Remember, how we added all post types in the hidden field. Well, we added it just so we can run the in_array check and keep the checkboxes checked. There was no documentation on how to do this otherwise, so this was the best way we found that does the job. Below is a preview of how the search box looks:

From there, the user can simply modify the parameter as they please.

Hopefully this article helped those in need. When we are doing the research, there were bunch of incomplete articles. Found the answer of adding the checkboxes in the WordPress support forum in an older thread however they were only talking about hidden fields. Whereas we wanted to give users the option to modify the search query. Props to @tammyhart for pointing us in the right way to do the checked query for the variable.

1 . Add Function Codes

here you can specify the archive-search.php

 function template_chooser($template)   
{    
  global $wp_query;   
  $post_type = get_query_var('post_type');   
  if( $wp_query->is_search && $post_type == 'products' )   
  {
    return locate_template('archive-search.php');  //  redirect to archive-search.php
  }   
  return $template;   
}
add_filter('template_include', 'template_chooser');    

2 . create search result template for custom post type ( search-archive.php )

 <?php
  /* Template Name: Custom Search */        
  get_header(); ?>             
  <div class="contentarea">
      <div id="content" class="content_right">  
               <h3>Search Result for : <?php echo "$s"; ?> </h3>       
               <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>    
          <div id="post-<?php the_ID(); ?>" class="posts">        
               <article>        
              <h4><a href="<?php the_permalink(); title="<?php the_title();     ?>"><?php the_title(); ?></a><h4>        
              <p><?php the_exerpt(); ?></p>        
              <p align="right"><a href="<?php the_permalink(); ?>">Read     More</a></p>    
              <span class="post-meta"> Post By <?php the_author(); ?>    
               | Date : <?php echo date('j F Y'); ?></span>    

              </article><!-- #post -->    
          </div>    

     </div><!-- content -->    
  </div><!-- contentarea -->   
  <?php get_sidebar(); ?>
  <?php get_footer(); ?>
Bạn thấy bài viết này như thế nào?: 
Average: 3.3 (4 votes)
Ảnh của Khanh Hoang

Khanh Hoang - Kenn

Kenn is a user experience designer and front end developer who enjoys creating beautiful and usable web and mobile experiences.

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Máy tính bảng Kindle Fire giá rẻ có gì hay?

Máy tính bảng Kindle Fire giá rẻ có gì hay?

Với mức giá cực thấp, Kindle Fire đang trực tiếp đe dọa iPad và trở thành thế lực mới trên thị trường.

Samsung Galaxy S III

Samsung Galaxy S III AT&T có thêm phiên bản màu đỏ

Như chúng ta đều biết, sản phẩm Galaxy S III đang được Samsung bán ra với 2 tùy chọn về màu sắc là trắng cẩm thạch (marble white) và xanh đá (pebble blue)

Sự kiện Drupal: Global Training Day - 14/12/2013

Sự kiện Drupal: Global Training Day - 14/12/2013

Hưởng ứng phong trào Thế gian tìm hiểu Drupal, nhóm Drupal Vietnam chúng ta sẽ tổ chức một workshop nho nhỏ để giới thiệu cộng đồng biết đến cái mã nguồn mạnh mẽ này.

Công ty diệt chuột T&C

 

Diet con trung