Custom Fields – Configuring Search Fields
This article explains how to configure search options in the [adverts_list], if you are looking for general advice on using the Custom Fields extension please see the Getting Started with Custom Fields article.
When you will create the first search form from wp-admin / Classifieds / Options / Custom Fields panel you will notice that its layout differs from the Create Ad form and mimics the design of a search form in [adverts_list] shortcode
The first thing you can notice is a dark line with a drag icon, it separates fields that are always visible in the form from fields visible after clicking the “Advanced Search” button.
When editing fields in this form you will notice the “Field Width” option which allows selecting if the field should take 50% or 100% of the available space.
The most important part of the configuration is the “Search” section, it allows to configure how the Ads database will be searched when this field is filled. The first field in the Search section is the “Search By” dropdown.
As you can see in the image above it has 4 groups:
- Advert Fields – these options do not require any additional configuration
- Dates – requires configuring how dates will be compared
- Taxonomies – requires configuring which taxonomy field you will be searching by and how matching should be done
- Custom Fields – requires configuring how the fields will be searched and how to compare values.
This is quite complex and getting the search right might be difficult at first, so below, I will explain in more detail how does the different search types work.
Once you select in Search By field that you want to search by one of the groups requiring configuration you need to fill a “Madlib” form, also known as natural language form, this is a kind of form which inputs are inside a sentence, feeling the inputs completes the sentence, you can learn more about mad libs here.
Currently, you can configure search by Publication and Last Modification date (the expiration date is a custom field so it is not really possible to have it there), if you will select one of the dates you will need to fill the following sentence in order to configure the search
Searching by date will usually work best with Date and Time, Text Input, and Dropdown fields.
There are 3 options to choose from only
- on – use this option if you want to show Ads posted on a specific date. That is “search for ads posted on [value entered in the search form]”.
- before – use this option if you want to display Ads posted before a specific date. That is “search for ads posted before [value entered in the search form]”
- after – use this option if you want to display Ads posted after a specific date. That is “search for ads posted after [value entered in the search form]”.
The typical use case is showing Ads posted within a date range, in this case, you would need to have two inputs searching by the same date field, one configured with the “before” option and the other with the “after” option.
If you configure two fields like on the image below your users will be able to search by publication date “from” and “to” selected dates.
ProTip: The search by date accepts relative search by date, so if you wish to search by ads posted in the last 7 days you can have the field configured to search “before” and as the search option enter “7 days ago”.
Taxonomies usually have predefined values in the wp-admin panel, so most of the time you will want to use Autocomplete, Radio, Checkbox, or Dropdown inputs to search for them, although the search can work with other inputs as well.
If in the “Search By” field you will select one of the taxonomies available in the “Taxonomies” group then the sentence (mad lib) to fill will look like on the image below:
In the first “-select-” field you select the taxonomy property you want to search by, allowed values are term id, title, term slug and term taxonomy id. Usually, you will want to select here term id, especially if you are configuring the Taxonomy Data Sources from wp-admin / Classifieds / Options / Custom Fields / Data Sources panel.
If you are using a text field (instead of a dropdown, autocomplete, or checkbox fields) it makes sense to search by title or term slug.
In the second field, you can select search operator or what will be considered a valid match, explaining it is a bit tricky, to make it easier let’s imagine we have three Ads each with different categories selected
- Car with taxonomies A, B, C
- Toy with taxonomies C, D
- Book with taxonomies D, E
I will use these imagined Ads and taxonomies when explaining search types, there are three options
matches one or more – use this to return only Ads that have assigned at least one category from the search, for example
- searching for B will return Car
- searching for D will return Toy and Book
- searching for A and C will return Car and Toy
matches none of – use this to exclude some taxonomies from search, for example
- searching for C will return Book
- searching for E will return Car and Toy
- searching for A and E will return Toy
matches all of – use this to return only Ads which have assigned all categories from the search, for example
- searching for C will return Car and Toy
- searching for B and C will return Car
- searching for A, C and D will return Toy
Include Ads from sub-categories as well
At the end of the Search group, there is an “Include Ads from sub-categories as well” checkbox. Checking it is only applicable if you are using tree-like taxonomies (for example multi-level categories), in other cases, it does not do anything.
If you decide to check this checkbox then when selecting an option in the search form WPAdverts will automatically search in this option sub-categories. In other words, selecting an option in the search form will have the same effect as selecting this option and ALL its sub-options.
Search By Category
By default, there is only an Advert Category taxonomy available in WPAdverts, the video below will explain how to use it to allow your users to search Ads by category
When searching by meta fields you will need to fill two short sentences
In the place of first red “- select -” you can enter one of the following values:
- is equal to – use if search field value should exactly match meta value
- is NOT equal to – use if search field value should be different than match meta value
- is greater than – use if search field value should be greater than meta value
- is greater or equal to – use if search field value should be greater or equal to meta value
- is smaller than – use if search field value should be smaller than meta value
- is smaller or equal to – use if search field value should be smaller or equal to meta value
- contains – use if search field value should contain match meta value
- does NOT contain – use if search field value should not contain match meta value
- has one or more selected – use this field only with multi-select fields (checkbox, dropdown)
- has none of selected – use this field only with multi-select fields (checkbox, dropdown)
In the second sentence, the selected value can be one of numbers, binary data, texts, dates, dates and time, decimal numbers. Which one you will select depends on what data is user entering in the search field, most of the time you will select either numbers or texts.
Below you can find a couple of video examples explaining how to configure the most common search types.
Note each video starts with creating a new [adverts_list] search form scheme, but if you already have a form scheme you can skip to the part where i am adding a new “Short Text” field to the form.
Search By Text
The below video shows how to search the Ads by “Contact Person” name, but if you have some other field which value is text (ie. a “Short Text”, “Text Area”, sometimes “Dropdown”) then the same method will work for you.
Search By Price Range
In the below video i am adding two fields that used together will allow searching by price range. The method used in the video you can apply to any other field which value is a number.