Home >CMS Tutorial >WordPress >How to Add an Advanced Search to Your WordPress Site
<span><span><span><form</span> method<span>="get"</span> action<span>="<?php bloginfo('url'); ?>"</span>></span> </span><span><span><span><fieldset</span>></span> </span><span><span><span><input</span> type<span>="text"</span> name<span>="s"</span> value<span>=""</span> placeholder<span>="search…"</span> maxlength<span>="50"</span> required<span>="required"</span> /></span> </span><span><span><span><button</span> type<span>="submit"</span>></span>Search<span><span></button</span>></span> </span><span><span><span></fieldset</span>></span> </span><span><span><span></form</span>></span></span>There’s nothing wrong with that and I thought it was the only option for many, many years. However, more advanced queries are possible without the aid of plugins (although plenty are available). Deep within the WordPress core, the application parses several other querystring parameters and uses them to return a more focused set of search results. There is some rudimentary documentation which lists the parameter names:
<span><span><span><form</span> method<span>="get"</span> action<span>="<?php bloginfo('url'); ?>"</span>></span> </span><span><span><span><fieldset</span>></span> </span><span><span><span><input</span> type<span>="text"</span> name<span>="s"</span> value<span>=""</span> placeholder<span>="search…"</span> maxlength<span>="50"</span> required<span>="required"</span> /></span> </span><span><span><span><button</span> type<span>="submit"</span>></span>Search<span><span></button</span>></span> </span><span><span><span></fieldset</span>></span> </span><span><span><span></form</span>></span></span>Our search form can allow the user to refine their search to specific categories:
http://yoursite.com/?s=search+term&category_name=kittensIf you’d rather list all categories, add the following code between the tags:
<span><span><span><form</span> method<span>="get"</span> action<span>="<?php bloginfo('url'); ?>"</span>></span> </span><span><span><span><fieldset</span>></span> </span><span><span><span><input</span> type<span>="text"</span> name<span>="s"</span> value<span>=""</span> placeholder<span>="search…"</span> maxlength<span>="50"</span> required<span>="required"</span> /></span> </span><span><span><span><select</span> name<span>="category_name"</span>></span> </span><span><span><span><option</span> value<span>=""</span>></span>all categories<span><span></option</span>></span> </span><span><span><span><option</span> value<span>="kittens"</span>></span>cute kittens<span><span></option</span>></span> </span><span><span><span><option</span> value<span>="puppies"</span>></span>adorable puppies<span><span></option</span>></span> </span><span><span><span></select</span>></span> </span><span><span><span><button</span> type<span>="submit"</span>></span>Search<span><span></button</span>></span> </span><span><span><span></fieldset</span>></span> </span><span><span><span></form</span>></span></span>
<span><span><?php </span></span><span><span>// generate list of categories </span></span><span><span>$categories = get_categories(); </span></span><span><span>foreach ($categories as $category) { </span></span><span> <span>echo '<option value="', $category->slug, '">', $category->name, "</option>\n"; </span></span><span><span>} </span></span><span><span>?></span></span>Your search from could therefore limit results to certain tags, e.g.
http://yoursite.com/?s=search+term&tag=cockroachSimilarly, you can generate a list of all tags for the select field:
<span><span><span><form</span> method<span>="get"</span> action<span>="<?php bloginfo('url'); ?>"</span>></span> </span><span><span><span><fieldset</span>></span> </span><span><span><span><input</span> type<span>="text"</span> name<span>="s"</span> value<span>=""</span> placeholder<span>="search…"</span> maxlength<span>="50"</span> required<span>="required"</span> /></span> </span><span><span><span><select</span> name<span>="tag"</span>></span> </span><span><span><span><option</span> value<span>=""</span>></span>any tag<span><span></option</span>></span> </span><span><span><span><option</span> value<span>="cockroach"</span>></span>cockroaches<span><span></option</span>></span> </span><span><span><span><option</span> value<span>="snake"</span>></span>snakes<span><span></option</span>></span> </span><span><span><span></select</span>></span> </span><span><span><span><button</span> type<span>="submit"</span>></span>Search<span><span></button</span>></span> </span><span><span><span></fieldset</span>></span> </span><span><span><span></form</span>></span></span>
<span><span><?php </span></span><span><span>// generate list of tags </span></span><span><span>$tags = get_tags(); </span></span><span><span>foreach ($tags as $tag) { </span></span><span> <span>echo '<option value="', $tag->slug, '">', $tag->name, "</option>\n"; </span></span><span><span>} </span></span><span><span>?></span></span>Note that I’ve used a PHP array parameter named taglist . You can use any name other than those already reserved by WordPress (see the list above). We can now intercept a search submission in our WordPress theme’s functions.php file. The advanced_search_query function detects whether a search is active then sets the WP_Query tag_slug__and parameter accordingly.
<span><span><span><form</span> method<span>="get"</span> action<span>="<span><?php bloginfo('url'); ?></span>"</span>></span> </span><span><span><span><fieldset</span>></span> </span><span><span><span><input</span> type<span>="text"</span> name<span>="s"</span> value<span>=""</span> placeholder<span>="search…"</span> maxlength<span>="50"</span> required<span>="required"</span> /></span> </span><span><span><span><p</span>></span>Refine search to posts containing chosen tags:<span><span></p</span>></span> </span><span><span><?php </span></span><span><span>// generate list of tags </span></span><span><span>$tags = get_tags(); </span></span><span><span>foreach ($tags as $tag) { </span></span><span> <span>echo </span></span><span> <span>'<label>', </span></span><span> <span>'<input type="checkbox" name="taglist[]" value="', $tag->slug, '" /> ', </span></span><span> <span>$tag->name, </span></span><span> <span>"</label>\n"; </span></span><span><span>} </span></span><span><span>?></span> </span><span><span><span><button</span> type<span>="submit"</span>></span>Search<span><span></button</span>></span> </span><span><span><span></fieldset</span>></span> </span><span><span><span></form</span>></span></span>Finally, we use the pre_get_posts action hook to run our advanced_search_query function before a query is executed:
<span>// advanced search functionality </span><span>function advanced_search_query($query) { </span> <span>if($query->is_search()) { </span> <span>// tag search </span> <span>if (isset($_GET['taglist']) && is_array($_GET['taglist'])) { </span> <span>$query->set('tag_slug__and', $_GET['taglist']); </span> <span>} </span> <span>return $query; </span> <span>} </span> <span>}</span>Adding advanced search facilities to WordPress is remarkably easy yet few developers realize it’s possible … perhaps because documentation and examples are a little sparse. I discovered it by accident so I hope you find this code useful in your next WordPress project.
Customizing search results in WordPress can be achieved by using the WP_Query class. This class allows you to define specific parameters to tailor your search results. For instance, you can set parameters to search only within post titles, exclude certain post types, or even search within custom fields. You can also use plugins like SearchWP that offer advanced search customization options.
Yes, tags can significantly improve search results in WordPress. By using the get_the_tags() function, you can retrieve the tags associated with a particular post. This can be used to create a more refined search experience, allowing users to search for posts with specific tags.
The search query parameter in WordPress can be changed using the ‘query_vars’ filter. This allows you to change the default ‘s’ parameter to something more specific to your needs. For example, you can change it to ‘search_term’ to make your URLs more user-friendly.
WP_Query is a class in WordPress that allows you to create custom queries and loops. It provides numerous parameters that you can use to customize your queries, such as post type, category, tag, author, and more. This makes it a powerful tool for creating advanced search functionalities.
Excluding certain post types from search results can be done using the ‘pre_get_posts’ action hook in conjunction with the WP_Query class. You can set the ‘post_type’ parameter to an array of the post types you want to include in the search results, effectively excluding all others.
Yes, you can search within custom fields in WordPress using the ‘meta_query’ parameter in WP_Query. This allows you to specify a custom field key and value, and return posts that match these criteria.
Improving search functionality in WordPress can be achieved by using plugins like SearchWP, Relevanssi, or Ajax Search Pro. These plugins offer advanced search features like partial matching, keyword stemming, and search weighting, providing a better user experience.
Yes, you can create a search form in WordPress without a plugin by using the get_search_form() function. This function generates the HTML for a search form, which you can customize to suit your needs.
The search query can be displayed in WordPress using the get_search_query() function. This function retrieves the search query string and can be used to display the search term on your search results page.
Yes, you can limit the number of search results in WordPress by using the ‘posts_per_page’ parameter in WP_Query. This allows you to specify the number of posts to display per page, effectively limiting the number of search results.
The above is the detailed content of How to Add an Advanced Search to Your WordPress Site. For more information, please follow other related articles on the PHP Chinese website!