Monday, May 2, 2011

Hongkiat.com: WordPress Search: Useful Plugins and Snippets

Hongkiat.com: WordPress Search: Useful Plugins and Snippets


WordPress Search: Useful Plugins and Snippets

Posted: 02 May 2011 05:36 AM PDT

WordPress is a powerful CMS tool not only powering blogs but countless forums and personal web pages. Many of the features offered are quite advanced for the market, yet their search still seems to be lagging. The functions offer a very simple solution for an extremely complex problem – finding the right content on your site!

Although the functions are great for searching out articles based on direct matches, the system falls short with many possible uses. More specifically the inability to search between all categories, tags, or even a specific category and/or tag. Similarly all posts are displayed on default by date, newest to oldest. This is a huge gap in UX, what about users who may be looking for popular articles with the most views or comments?

wordpress search Wordpress Search: Useful Plugins and Snippets

Below I’ve offered a brief look into WordPress’ search features and how they work within the system. Understanding how everything runs out of the box will make manipulating searches much easier. Additionally I’ve added a few powerful plugins and code snippets desirable for any WP website.

the Basic of WordPress Search

When running a search query through WordPress all results are returned based on publication time. This would include pages, which would be great, if WordPress has set the ability to do so. Two great plugins Search Unleashed and Search Everything provide fixes allowing users to search through pages and comments as well. One major problem is how WordPress ignores the power of keywords within search.

wordpresscom search Wordpress Search: Useful Plugins and Snippets

If an article was published a year or two ago the odds of it being found in a search are slim to none. This is unless the user is entering the keywords they want into a larger engine such as Google or Bing. When you search for “web design” WordPress is looking to match for exactly that. WordPress developers may be working on updates, but such a query would not return results containing simply design.

Similarly what about post categories and tags? These can be matched in keywords and throw off an entire search. The distinct functions behind WordPress’ search are prehistoric compared to most, which thankfully the system can be openly updated from within the development community.

WordPress Theme Files

Inside each WordPress theme folder is a set of search files. These appear to be useful for functionality and powerful search forms. Inside the root template file search.php you will find the general template for search results.

Many times I’ll hear developers fabricating the mistake of including their search.php inside another core file, such as page.php or single.php. This is a strong technique for building modular templates, however the straight search file is used for displaying pagination and results only. The standard file name searchform.php is what would include some basic PHP code for calling search query data. The rest of the file is a straight HTML form including one (1) input field and a submit button.

wp theme files Wordpress Search: Useful Plugins and Snippets

This file is often included in the heading or sidebar area of templates. It offers an elegant solution to include a ready-made form and users can take advantage of the many powerful search techniques offered in WordPress. From the many new attributes in HTML5 it’s possible to offer default text inside the input field such as “search…” or “enter terms here”.

When entering in data to display your search form, the simplistic routine may happily surprise you. There is a simple function written get_search_form() which can be added anywhere in your templates to display the contents of searchform.php. This is an internal function developed by WordPress and used to make development for search functionality easy as pie!

WP Query Function

There is a function written into WordPress’ backend which can be utilized for direct SQL queries. WP_query() has been used by WordPress developers and theme designers alike to create custom search queries more complex than WordPress’ default.

wp query codex Wordpress Search: Useful Plugins and Snippets

If you’re a developer I recommend reading through the function reference page for a bit of insight on the methodology. The documentation is very long and probably won’t be used by many. There are some real neat features such as pulling specific posts or categories based on which content is currently displayed in-page.

The Query function also allows for checking against the current page value. WordPress automatically gives a name towards each type of page on your site. Blog posts, pages, search results, and home are just a few examples. Below I’ve outlined a brief list of common page variables for those interested in examining beneath the surface.

  • $is_single – viewing a single post page
  • $is_author – viewing an author post directory page
  • $is_search – viewing a search results page
  • $is_category$is_tag – viewing a list of posts by category or tag
  • $is_404 – viewing 404 error page

16 Plugins to Enhance Search

Below I’ve included links to a few popular plugins related to search and queries. These are all free and offered for download from WordPress’ official extensions directory. I’d highly recommend against installing more than 2 or 3 of these at a time – read up on the descriptions and test one-by-one to see if there’s anything which perfectly suits your blog!

Google Custom Search Plugin
The default option for searching in WordPress isn’t really the best solution. Oftentimes webmasters would rather funnel their search queries through Google for quicker and more targeted results. After installation this plugin will automatically rewrite the default WordPress search form with a custom Google Search. Adopts a new set of friendly URLs on-the-fly!

Google Custom Search Plugin Wordpress Search: Useful Plugins and Snippets

Enhanced Search Form
By default the WordPress search form is a standard input field. This is great for basic queries involving specific keywords, but for advanced users the default options fall short. Enhanced Search Form will dynamically generate an XHTML form which accepts new search terms such as Boolean AND statements.

Enhanced Search Form Wordpress Search: Useful Plugins and Snippets

Search Everything
Search Everything is another great all-in-one plugin for supporting your advanced searching needs. A few of the most popular features include search highlighting, custom taxonomies, browsing approved comments and many more! The administration panel is very simple and setup is a breeze.

Search Everything Wordpress Search: Useful Plugins and Snippets

WordPress Sphinx Search Plugin
The Sphinx server can offload the heavy search queries from your server into other remote settings. Upon activation you’ll notice super-fast speeds and are able to sort results according to freshness and relevance. Additionally the plugin is capable of displaying a sidebar widget of most recent and top related search keywords.

WordPress Sphinx Search Plugin Wordpress Search: Useful Plugins and Snippets

Search Meter
Search Meter is a fascinating idea for those webmasters interested in tracking analytics. Every search query is stored and archived in the admin panel with extra details examining search analytics. You’ll be given data about how many searches were failed or turned up no results, as well as popular and recent search terms. The plugin will generate statistics which you can reset or export for examination.

search meter plugin Wordpress Search: Useful Plugins and Snippets

Fast WordPress Search
Fast WordPress Search is a basic replacement plugin for WordPress’ default engine. This will generally return more relevant pages and slightly speeds up the process. The process was written to work with WP’s vast library of functions to reduce database calls and return quicker results for intense queries. The install is also accompanied by benchmark tools to compare times.

fast wordpress search Wordpress Search: Useful Plugins and Snippets

Amazon Search Widget
If you work with Amazon’s affiliate program then you’ll love this next plugin. With a few simple steps and a single template edit it’s very convenient to implement a Flash-based search form. This will search within Amazon’s library to pull data about products and new releases. From here it’s a simple process of entering your affiliate ID to start earning money from your blog searches!

amazon search widget Wordpress Search: Useful Plugins and Snippets

Looser Search Plugin
Here we have a small plugin with great expectations right after install. The Looser Search Plugin modifies already built-in processes from within WordPress libraries to match keywords instead of full terms. An internal dictionary of common English words is skipped over to speed up the search process. If you’re looking for a basic plugin to install and get the quickest results I’d highly recommend this one.

looser search plugin Wordpress Search: Useful Plugins and Snippets

Dave’s WordPress Live Search
If you are a fan of Microsoft Live Search this simple plugin will amplify your blogging experience. Offering results from Live Search will mean higher relevancy and quicker load times than internal processing. The plugin is made to provide instant up-to-date results as the user types – all powered behind the scenes with jQuery and some basic CSS styles. Try installing and see if your blog can handle the page load, as it provides an amazing user experience to search results.

daves wordpress live search Wordpress Search: Useful Plugins and Snippets

Search Tag Cloud
This plugin provides you with easy access to develop a simple tag cloud. This will result in great rankings from Google as more in-links will be leading to your blog pages. Additionally the user experience is dramatically increased when you consider how many posts can be found with just a few clicks. The plugin requires standard installation steps and has been developed with SEO in mind.

search tag cloud Wordpress Search: Useful Plugins and Snippets

Highlight Search Terms
You may have seen this plugin being adopted in countless blogs today. Whenever a visitor finds your page through a major search engine (Google, Yahoo!, Bing, Lycos, Ask…) each of the keywords will be highlighted in your content. This helps visitors figure out where the page content is located and what reference frame it’s in. By default there are no core CSS styles, so you’ll have to design these yourself after activation.

wp highlight search terms Wordpress Search: Useful Plugins and Snippets

Better Search
Better Search, as the title implies, is a standard plugin to give your WordPress blog better search. Each results page is split based on keyword relevancy and advanced techniques for recognizing meta tags, post tags, and categories. When typing the new search form will display the most popular search terms being searched throughout your blog. This is updated frequently based upon how much traffic your search queries bring in!

better search options Wordpress Search: Useful Plugins and Snippets

Search Light
As you may have seen many places elsewhere the update-as-you-type functionality has exploded. Since the release of Google Instant many other search providers have been rolling out similar techniques. Search Light is a fantastic plugin which uses an Ajax dropdown interface to create dynamic menus of related queries. It’s also possible to tie in your post thumbnails and total number of results inside the search bar itself.

wordpress search light Wordpress Search: Useful Plugins and Snippets

WP Instant Search
This plugins requires a few external libraries, although offers similar functionality as the previous Search Light. If you really enjoy Ajax dropdown suggestions you’ll find plenty of the same features here with WP Instant Search. The plugin is updated to the most recent version WordPress 3.0.5 and will check against WordPress tags, posts, pages, and categories.

wp instant search Wordpress Search: Useful Plugins and Snippets

WP E-commerce Product Search Widget
This plugin supports widget displays for an e-commerce solution running over WordPress. When you’re selling items or even software online it’s important your e-commerce solution is simple to navigate and products are easy to find. With this nifty plugin we can replace WordPress’ stale search functionality to include a new query view. Results pages will list products in a grid-style layout and holds compatible up to the most recent release.

wp ecommerce product search Wordpress Search: Useful Plugins and Snippets

ThreeWP Ajax Search
A no-nonsense plugin for Ajax searches. Just download and install the plugin to get a sense of how easy the process will unfold! Default settings work perfectly with the Twentyten theme and all derivatives. One cool feature is how this plugin still works around WordPress’ default search engine. In this case you don’t lose anything from WordPress’ powerful library and instead only gain magnificent front-end experience effects. There are many options for customization including CSS styles and jQuery speeds and animation styles.

threewp ajax search plugin Wordpress Search: Useful Plugins and Snippets

5 Useful Search Snippets

1. Exclude Post/Page from Search Results

The following function, allows you to exclude posts of any categories, or even pages out of the search results. (via wprecipes)

(functions.php)

 function SearchFilter($query) {     if ($query->is_search) {         $query->set('cat','0,1');     }     return $query; }  add_filter('pre_get_posts','SearchFilter'); 

2. Searching a specific Category

Return search results from a specific category.

(functions.php)

 function SearchFilter($query) {   if ($query->is_search) {     // Insert the specific categories you want to search     $query->set('cat', '8,9,12');   }   return $query; }  add_filter('pre_get_posts','SearchFilter'); 

3. Searching a specific post type

Filter out all other post types and target your search to a specific WordPress post type. (via ashbluewebdesign)

(functions.php)

 function SearchFilter($query) {   if ($query->is_search) {     // Insert the specific post type you want to search     $query->set('post_type', 'feeds');   }   return $query; }  // This filter will jump into the loop and arrange our results before they're returned add_filter('pre_get_posts','SearchFilter'); 

4. Highlight WordPress Search Terms (jQuery)

Highlights search terms in WordPress result page. (via weblogtoolscollection)

(functions.php)

 function hls_set_query() {   $query  = attribute_escape(get_search_query());    if(strlen($query) > 0){     echo '       <script type="text/javascript">         var hls_query  = "'.$query.'";       </script>     ';   } }  function hls_init_jquery() {   wp_enqueue_script('jquery'); }  add_action('init', 'hls_init_jquery'); add_action('wp_print_scripts', 'hls_set_query'); 

(header.php), before </head>

 <style type="text/css" media="screen">     .hls { background: #D3E18A; } </style>  <script type="text/javascript"> jQuery.fn.extend({ highlight: function(search, insensitive, hls_class){   var regex = new RegExp("(<[^>]*>)|(\\b"+ search.replace(/([-.*+?^${}()|[\]\/\\])/g,"\\$1") +")", insensitive ? "ig" : "g");   return this.html(this.html().replace(regex, function(a, b, c){ 	return (a.charAt(0) == "<") ? a : "<strong class=\""+ hls_class +"\">" + c + "</strong>";   })); } }); jQuery(document).ready(function($){ if(typeof(hls_query) != 'undefined'){   $("#post-area").highlight(hls_query, 1, "hls"); } }); </script> 

5. Display Search Term + Result(s) Count

Return search queries and number of results. Example – Search Result for twitter – 8 articles. (via wpbeginner)

 <h2 class="pagetitle"> 	Search Result for 	<?php /* Search Count */ 	$allsearch = &new WP_Query("s=$s&showposts=-1"); 	$key = wp_specialchars($s, 1); 	$count = $allsearch->post_count; 	_e(''); _e('<span class="search-terms">'); 	echo $key; _e('</span>'); _e(' &mdash; '); 	echo $count . ' '; _e('articles'); wp_reset_query(); 	?> </h2> 

(bellefoong)


Advertise here with BSA

No comments:

Post a Comment