A custom search allows people to find exactly what they are looking for in WordPress sites. Toolset makes it easy to build searches for any type of content.
With Toolset, you choose what to search by, how to style the search box, and how to display the results.
You can also enable pagination, AJAX updates, and sorting to enhance the custom search and make it more usable for sites that have a lot of content.
Besides this you can display custom search results on a map, or set up the search box and results results side-by-side.
- Steps for creating custom searches
- Make the search results update when search inputs change
- Add front-end sorting to search results
- Add pagination to search results
- Style the search box and results
- Display the search box and search results on different pages
- Display the search box and search results side-by-side
- Hide the search results until the first search
- Display the search results on a map
- How to search by post relationships
- Enable custom search for existing Views
There are two main steps for creating a custom search:
1. Create the custom search box
2. Design the custom search results
It doesn’t matter which of these steps you do first. However, having at least simple search results makes it easier to test the search form.
Here is an example of a complete custom search, with both the box and the results.
You start by inserting a View block into your page.
In the View creation wizard, enable the Search option.
After finishing the View creation wizard there is a View Search section in the View’s editing area. Use the available buttons to add a custom search field, insert submit and reset buttons, or add other blocks.
For example, you can click to Add Search Field and use the Select a Field dropdown. Select by which content type you want users to be able to search by and click Next.
You can now use the right sidebar to tweak the options for this search field. This includes the main field settings, label and style options.
The results of a custom search is basically a list of posts. In Toolset, we call such lists Views. Instead of writing custom queries, Toolset does everything for you. All you need to do is visually design what and how you want to display for each post.
You create a View using Toolset blocks as they allow you to display dynamic content coming from custom fields, taxonomies, and standard post fields. You can then use the styling options within each Toolset block to design how the list will look like.
You can even display Dynamic Sources using WordPress core blocks and popular block plugins. Insert them and in the right sidebar, expand the Dynamic Sources section. Toggle on the option for any of the supported fields in that block.
Toolset even allows you to display Dynamic Sources inside any text blocks like headings, buttons, and more. For more information, check out the lesson about displaying Inline Fields.
You can also enhance search results by adding pagination, front-end sorting, or making results update when search inputs change.
For more information see below and in the lesson about creating custom lists of posts.
By default, after selecting the search parameters, users need to click a submit button to see a list of posts that match their search.
You can easily make the search results update when search inputs change, without reloading the page:
- Select the main View block and in the right sidebar, expand the Custom Search section.
- Select the AJAX refresh when changing any filter option.
Note that your form doesn’t need a submit button when using this option.
You can allow users to sort the results of the custom search.
You can enable sorting when creating a new View by toggling the Front-end sorting option.
To add sorting controls to an existing View use the following steps:
- Click to select the View block and click the Add Sorting button. You can find it at the top and bottom of the View Output block.
- Click the sorting control that appears and use the right sidebar to select the options.
You can select what to sort by, adjust the labels, tweak the styling, and more.
Search results could have a long lists of results. Pagination lets you breaks such lists into pages so visitors can switch between them without losing the search query.
By default, pagination is turned on when creating a new View. You can click the pagination controls and tweak the options using the right sidebar.
If you have an existing View without pagination you first have to enable it. To enable pagination, select your View block in Block navigation and expand the Pagination section in the right sidebar. There, click the option to enable pagination and tweak the options.
Toolset blocks allow you to design and style everything inside the block editor.
You design a View with a custom search using different blocks. They will have different styling options. The most common styling options are for Toolset blocks are:
- Typography – change the appearance of blocks that output text
- Style Settings – change colors, margins, paddings and borders, drop shadows and more.
- Advanced – block allignment, hide element, and more.
Besides styling the blocks themselves, there are two Toolset blocks that help you design your Views and its custom search:
- Toolset Container – group multiple blocks and apply styling to them: add a background, margins, paddings, and more.
- Toolset Grid – display elements side-by-side in any type of columns: adjust column width, number of columns, and more.
To learn all about this topic visit the lesson about styling custom searches.
Sometimes, you might need to display a custom search box on one page and show the results on another.
To do this, you need to create a View in the “results” page and insert that View again where you want to show only the search box (like on the site’s homepage).
See the full details in the lesson about displaying search results on a different page.
We’ll use the Toolset Grid block to position the search box and search results side-by-side. The trick is to insert the Grid block into the View Block and then drag the View Search and View Output sections into the grid cells.
- Scroll down to the bottom part of your View.
- Use the plus icon inside your View to insert the Toolset Grid block. Make sure you are inserting your Grid block inside your View.
- Use the Grid block up arrow to move up your Grid block to the top of your View.
- Grab the View Search block and move it inside the left cell of your grid.
- Grab the View Output block and drag it into the right-hand cell.
- Use the Block navigation to make sure your View structure looks like this:
- Shift your column sizes to give more room to the search results.
- Hover over the grid until the column line appears.
- Grab it and slide it over a bit to make your right-hand cell wider.
By default, a View with your custom search will display all initial results right away. You can customize the View so that it displays results only after users select and submit some search parameters.
You can do this by wrapping the Loop Output section of the View in a Conditional block, where the condition checks for the presence of a URL parameter. This parameter is always added when filtering a View, but it isn’t present when the page loads for the first time.
Use the following steps to hide initial results:
- At the top of the existing View add the Conditional block. It is very important to place the Conditional block within the View Output block, but outside of the View Loop block.
- Select the Conditional block and in the right sidebar, click the Edit conditions button.
- Turn on the Advanced Editor and enter the following code into it:
- Click the Accept button.
- Now that the condition is set, you can drag-and-drop the View Loop block into the Conditional block.
- Update the page.
When you visit the page with the View on the front-end you should see only the search fields. The search results will appear only after selecting and submitting some search options.
The View with your custom search can also display items on a map. As visitors use the custom search, the map will automatically update to display only items that were returned by the search.
You can also easily display custom search and the map side-by-side like many popular websites.
Use the following steps to add a map to a View:
- Edit the page that holds your View with a custom search and insert the Toolset Map block.
- In the right sidebar, scroll down to the section for the map marker.
- In the Source for the marker option, select View block and make sure your View with a custom search is selected.
For a video tutorial and more information see the lesson about displaying a list of posts on a map.
Toolset allows you to create relationships between different post types and connect posts. You can easily add custom search filters based on such relationships.
For example, a real estate site might have post types for properties and cities. When users search for properties, they can easily filter them by the city they’re interested in.
Follow these steps:
- Edit the page that holds your View with a custom search.
- In the View editing area, click the Add Search Field button.
- In the dropdown that appears select the Post relationship or repeatable field groups owner option.
- In the right sidebar, expand the Field Settings section and use the dropdown menu to select the relationship you want to add a filter for.
That’s it, the search will now include a field by the relationship you selected.
To learn more about this check out the lesson about what post relationships are and how they work.
When creating a new View, the Search option is disabled by default.
However, you can turn on the custom search feature for existing Views at any time:
- Click to select the main View block.
- In the General section (expanded by default), toggle the Search option.
A section called View Search will appear in the main editor.
Erstellen Sie benutzerdefinierte Beitragslisten