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? 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 SearchWhen 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. 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 FilesInside 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. 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 WP Query FunctionThere is a function written into WordPress’ backend which can be utilized for direct SQL queries. 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.
16 Plugins to Enhance SearchBelow 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 Enhanced Search Form Search Everything WordPress Sphinx Search Plugin Search Meter Fast WordPress Search Amazon Search Widget Looser Search Plugin Dave’s WordPress Live Search Search Tag Cloud Highlight Search Terms Better Search Search Light WP Instant Search WP E-commerce Product Search Widget ThreeWP Ajax Search 5 Useful Search Snippets1. Exclude Post/Page from Search ResultsThe 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 CategoryReturn 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 typeFilter 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 <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) CountReturn 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(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?> </h2> (bellefoong) |
You are subscribed to email updates from hongkiat.com To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
0 comments:
Post a Comment