I originally developed this plugin for a client project. Basically the problem they had was that they were storing structured data (real estate properties) but the default wordpress search didn’t allow them to search it in a structured way (by price and location). The plugin I’ve built allows you to configure multiple search input fields, configure the behaviour of each field (eg what options appear in a dropdown) and configure what database fields each input corresponds to (eg post title, post content, a custom field called price).
The plugin is now at point where I think it could be useful to other people although I’m aware that there are a lot of improvements that could be made. Please let me know if you have any suggestions or complaints and I will do my best to get them resolved.
You can download the plugin here.
- PHP Versions 4.4.9 and 5.2.6
- WordPress 2.7
Quick Start Guide
I will try to put more complete documentation together at some point but for now this is a quick start guide to building a search form.
- Install the plugin. Download it here unzip it in your wp-content/plugins folder, then activate it in the plugins page of your wordpress admin site.
- Add the widget to the sidebar on which you want it to appear.
- Click edit and set-up the fields for your form. (More Info Below)
Setting up the fields
Each field has a number of settings which control the way the input appears to the user and the way the search is performed.
Label sets the label displayed next to the field, you can set this to whatever you want.
Data Type controls what type of data is being searched (i.e. what database table is being used). The standard options are “Post Field” for data such as post title and post content, from the standard wordpress post data, “Custom Field” for data from the custom fields, and “Category” to search on the categories a post is in.
Data Field makes a more specific selection from the data available. The drop down list should give some sensible options, or if you know the name of the database field you want to query you can type this in manually.
Widget controls what type of HTML input is created for the front end of the site. It should be fairly obvious what these do. The inputs called “Auto Values” will automatically create options based on what is in the database.
Some widgets will require extra options in the Widget Options field. At the moment this is just the drop down and radio button widgets (not the auto values ones) which require you to specify a list of values for the user to choose from. You can specify this as a comma separated list of values, ‘a,b,c’ for three options with values ‘a’, ‘b’ and ‘c’ respectively, or if you want to give ‘friendly’ labels to the user you can separate the value from the label using a colon as follows ‘a:Group A,b:Group B,c:Group C’.
The Compare field controls the way that the user input is compared to the data in the database. I would expect the most commonly used of these to be “equals” which requires an exact match between the user data and the database field (useful for category searches) and the “Words in” or “Phrase In” types which will search to see if the user input is a part of the data, rather than a full match, this is useful for things like searching the text of a post. The difference between ‘words’ and ‘phrase’ is that ‘words’ splits the input into a series of words and searches for these individually whereas ‘Phrase’ searches for all the words in sequence.
The “Less Than” and “More Than” comparisons ensure that the data is less than or more than the user input respectively. This is probably only relevant for numeric input but it can be used for strings as well and will search alphabetically. For numeric input it can help to also tick the “Numeric” checkbox as sometimes the database will be set up to compare numbers alphabetically (so that 10 is less than 2).
The “Range” comparison is a little more complicated and requires the user input to be of the form A-B to return results that are between A and B. This is primarily intended to be used for Drop-downs and radio buttons where the values can be set by the site administrator. If you specify the values for the dropdown like the following: “-10:Less Than 10,10-20:Between 10 and 20,20-:More than 20″, then the user will be presented with the options “Less Than 10″, “Betwen 10 and 20″ and “More than 20″ but the search will be done with the values “-10″,”10-20″ and “20-”. Again the numeric checkbox should be used for numeric data.
I hope that helps, please let me know if you have any trouble (or joy) with the plugin or if any of my instructions don’t make sense.
For developers, I’ve tried to build this in a modular way to allow extensions to be added, I will try to document this at some point in the future but if you can’t wait then the best places to get started would be to look at the filter ‘custom_search_get_classes’ in custom-search/custom-search.php and the Great Real Estate bridging code in custom-search/bridges/greatrealestate.php.