\n \n\n
function feed_dir_rewrite( $wp_rewrite ) {\n$feed_rules = array( 'search\/(.+)' => 'index.php?s=' \n. $wp_rewrite->preg_index(1));\n$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;\n}\nadd_filter( 'generate_rewrite_rules', \n'feed_dir_rewrite' );\n<\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\n\nChange destination URL on the third line to whatever you like<\/li>\n\n\n\n Save changes<\/li>\n<\/ol>\n\n\n\nAfter you\u2019ve done these changes to your functions.php file, you can try out your new URL slug. Don\u2019t worry; if a plugin or a user tried to get access to your search result by navigating to old URL slug, your website won\u2019t return an error but simply redirect the search result to the new URL.<\/p>\n\n\n\n
If you want to change the slug to something unique, you can do that on the third line of the code.<\/p>\n\n\n
\n \n\n
This one will take your search results page to – www.yourdomain.com\/search\/search-term<\/em><\/p>\n\n<\/div>\n\n\nNow you can relax and stop thinking about how search results are displayed to your users as well as to search engine bots who will now more easily crawl through those results. Also, isn\u2019t it somehow more natural to see the whole \u201csearch\u201d word instead of that weird question mark followed by the letter \u201cs\u201d? Tell us what you think.<\/p>\n\n\n\n
Search posts exclusively by their titles<\/h2>\n\n\n\n If you\u2019re still using the default WordPress search engine, sooner or later you will want to modify how it works. While there\u2019s nothing wrong with the way it searches for your posts, you might want to limit the search to titles.<\/p>\n\n\n\n
Instead of going through all of the content, this little function will search only through post titles. In some cases, this might really help you get cleaner and faster search results:<\/p>\n\n\n\n
\nOpen functions.php<\/li>\n\n\n\n Copy and paste this code:<\/li>\n<\/ol>\n\n\n\n \n\n
function __search_by_title_only( $search, &$wp_query )\n{\nglobal $wpdb;\nif(empty($search)) {\nreturn $search; \/\/ skip processing - no search term \nin query }\n$q = $wp_query->query_vars;\n$n = !empty($q['exact']) ? '' : '%';\n$search =\n$searchand = '';\nforeach ((array)$q['search_terms'] as $term) {\n$term = esc_sql($wpdb->esc_like($term));\n$search .= \"{$searchand}($wpdb->posts.post_title LIKE \n'{$n}{$term}{$n}')\";\n$searchand = ' AND ';\n}\nif (!empty($search)) {\n$search = \" AND ({$search}) \";\nif (!is_user_logged_in())\n$search .= \" AND ($wpdb->posts.post_password = '') \";\n}\nreturn $search;\n}\nadd_filter('posts_search', '__search_by_title_only'\n, 500, 2);<\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\n\nSave changes<\/li>\n<\/ol>\n\n\n\nAfter the changes, you can open your website and start a new search. To be sure everything\u2019s working as it should, you should try searching for a word you know is in one of your post titles which should return the post on the results page. If you try to search for a word or a complete term that isn\u2019t contained in any title which you have published, search results will come out empty.<\/p>\n\n\n\n
And that\u2019s actually all there is. If you ever decide you want the old search back, simply erase or comment out the function.<\/p>\n\n\n\n
Improve the search function and find results within a category<\/h2>\n\n\n\n The default search function in WordPress is getting better and better with every update. You can easily include a search feature on your blog and allow people to easily find the content they want. But if you haven\u2019t made any changes to the function, search results will display everything related to the query. And that might be a problem if you have a lot of content because people will have tons of text on the results pages which won\u2019t help them a lot.<\/p>\n\n\n
\n
<\/figure><\/div>\n\n\n<\/p>\n\n\n\n
In this part of the article, we will show you how to allow your visitors to search only within specific categories which you can define in the code or allow visitors to select ones from the drop-down list.<\/p>\n\n\n\n
Select categories in advance:<\/h3>\n\n\n\n First, we will show you the code which will create a new search box. With this code, you won\u2019t let people select categories for their search but you will define categories within the code. This might help you with special categories which need a separate search box.<\/p>\n\n\n\n
For example, you may have a category that contains only smartphone reviews in it. With this code, you can allow people to search only through that category so that the search results don\u2019t load information about home appliances or other stuff you have on the website. Makes sense, right?<\/p>\n\n\n\n
\nOpen a page where you want the search box to appear or make a new widget from it<\/li>\n\n\n\n Copy and paste the following code:<\/li>\n<\/ol>\n\n\n\n \n\n
<form method=\"get\" id=\"search form\" action=\"\/\">\n<div>\n<input type=\"text\" value=\"\" name=\"s\" id=\"s\" \/>\n<input type=\"hidden\" value=\"1\" name=\"cat\" id=\"scat\" \/>\n<input type=\"submit\" id=\"search_submit\" name=\"Search\"\nvalue=\"Search\"\/>\n<\/div>\n<\/form><\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\n\nChange the value on the third input type to category ID which you want to search<\/li>\n\n\n\n Save changes<\/li>\n<\/ol>\n\n\n\nAfter you have made these changes, a new search box will appear and it will allow you to search through the category which ID you have entered in the code above. If you want to search through multiple categories, simply add more IDs by entering numbers separated by commas.<\/p>\n\n\n
\n \n\n
<input type=\"hidden\" value=\"1, 2\" name=\"cat\" id=\"scat\"\/><\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\nWhile this might be a great solution in some cases, there will be times when you will want to show a list of categories and allow visitors to choose where to search from.<\/p>\n\n\n\n
Let visitors choose a category to search from:<\/h3>\n\n\n\n Following the same example where you have categories like smartphones, desktop computers, laptops, home appliances, etc. why wouldn\u2019t you allow a visitor to choose one of those and search only through the “laptops” category, for example?<\/p>\n\n\n\n
In the following lines, we will show you how to do that:<\/p>\n\n\n\n
\nOpen sidebar.php or wherever you want the search box to appear<\/li>\n\n\n\n Copy and paste the code:<\/li>\n<\/ol>\n\n\n\n \n\n
<form method=\"get\" action=\"<?php bloginfo('url'); ?>\">\n<fieldset>\n<input placeholder=\"search category\" type=\"text\" \nname=\"s\" value=\"\" maxlength=\"40\" required=\"required\" \/>\n<select name=\"category_name\">\n<option value=\"smartphones\">Smartphones<\/option>\n<option value=\"desktop-computers\">Desktop Computers\n<\/option>\n<option value=\"laptops\">Laptops<\/option>\n<option value=\"home-app\">Home Appliances<\/option>\n<\/select>\n<button type=\"submit\">Search selected category<\/button>\n<\/fieldset>\n<\/form><\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\n\nChange categories under option values. Write category slug in value and a name between tags<\/li>\n\n\n\n Save changes<\/li>\n<\/ol>\n\n\n\nAutomatically show all categories on the list:<\/h3>\n\n\n\n If you want to show all categories and you don\u2019t want to worry about adding or removing ones from this code once you add\/delete them in your admin panel, use this code between <select><\/select> tags:<\/p>\n\n\n
\n \n\n
<?php\n$categories = get_categories();\nforeach ($categories as $category) {\necho '<option value=\"', $category->slug, '\">', \n$category->name, \"<\/option>n\";\n}\n?><\/pre>\n\n<\/div>\n\n\n<\/pre>\n\n\n\nThis code will generate a list of all available categories.<\/p>\n\n\n\n
And that\u2019s it. Now you can easily add one or more categories which your visitors can easily search through. Whether you want people to select the category themselves or you want to specify those in the code, you are covered.<\/p>\n\n\n\n
Define categories for WordPress search results<\/h2>\n\n\n\n By default, WordPress searches for everything on your site. If a user types a word or the entire phrase in a search box, he will get results from the entire website and that includes posts, pages, categories and tags, titles and content, etc. If you haven\u2019t installed any other search system, or already have made changes, you may want to modify the default search box.<\/p>\n\n\n
\n
<\/figure><\/div>\n\n\n<\/p>\n\n\n\n
If you want to modify the search box which already exists in your WordPress and define categories which will be included in the result pages, you may take a little different approach.<\/p>\n\n\n\n
In this part, we\u2019re about to show you how to do that. You can easily add or remove categories from the code and decide which one you (don\u2019t) want in your search results.<\/p>\n\n\n\n