In AnQi CMS, configuring custom fields as filter conditions is a very practical feature to make your website content more dynamic and interactive.It can help users quickly find the content they need based on specific attributes, thereby significantly improving the user experience of the website.Let's learn together how to implement this feature in AnQi CMS.
Custom field: 'Living' tags of content
Firstly, we need to understand what a custom field is. In Anqi CMS, a custom field is an additional data item you add for a specific content model (such as articles, products, etc.).They can be text, numbers, single selection, multiple selection, dropdown menu, and more types.For example, if you operate a real estate website, you can add custom fields such as "type", "area", "location", etc. to the "House" content model;If you manage a job recruitment website, you can add fields such as "city", "education", "work experience", and so on.
These custom fields are not only used to store and display additional information, but also play a crucial role in becoming the key to users filtering content.By configuring it as a filter condition, you can upgrade your website from a simple content display platform to a powerful information retrieval tool.
Turn on the filtering feature: Backend configuration is key
Translate the custom field into a filter condition, the first step is to set it in the Anqí CMS backend management interface.You need to enter the model management page corresponding to the filter settings you want to configure.For example, if your article model needs to filter based on 'author' or 'publishing area', you will enter the field management of the article model.
In the model field settings, you will see that each custom field has a name called "Is filtering parameter" (is_filterThe option. This option is the key to the filtering function.After you check this option and save, AnQi CMS will recognize this custom field as a condition that can be used for filtering content lists.
It is worth noting that for fields with predefined options (such as radio buttons, checkboxes, dropdown menus), the values you enter when creating the field will automatically become specific options in the frontend filter.For example, if you set values such as 'Beijing, Shanghai, Guangzhou' for the 'City' field, then these city names will appear as options in the filter.
Get filter conditions: dynamically present user options
How does the front-end page know what filtering conditions and specific options are available after your custom field is configured for filtering? At this point, it is necessary to use the Anqi CMS providedarchive/filtersinterface.
By usingarchive/filtersThe interface sends a GET request and includes the filtering conditions you want to obtainmoduleId(Model ID), it will return a list containing all searchable fields. Each field will have its display name (name) and the actual name used in API calls (field_nameAnd a series of specific filtering options (items)
For example, if you request the filter conditions for model ID 1, the returned data may include two filter fields such as "City" and "Education", and each field lists specific options such as "Beijing", "Shanghai", "Master", and "Bachelor".This information can help you dynamically generate a filter form on the website front-end, where users can select predefined conditions without manual input.
Apply filter: Accurately obtain the desired content
After obtaining the list of filter conditions, the next step is how to apply these conditions to the actual content query to obtain a more accurate list of document lists.archive/listThe interface is the core to achieve this goal.
When callingarchive/listthe interface, in addition to the conventionalmoduleId/categoryId/orderparameters, you can also directly pass custom fields.field_nameAs a URL query parameter name, and pass the value of the option selected by the user as a parameter.
For example, if you want to find all articles with model ID 1 and "city" as "Beijing", your API request URL might look like this:
{域名地址}/api/archive/list?moduleId=1&city=北京
If you need to further filter the content with a 'Bachelor's Degree', you can continue to add parameters:
{域名地址}/api/archive/list?moduleId=1&city=北京&certificate=本科
By such a combination of parameters,archive/listThe interface returns only the content list that meets these filtering conditions, greatly enhancing the efficiency and usability of information search by users.
Summary
By following these steps, Anqi CMS's flexible custom field configuration combined with powerful API interfaces makes it easy to build websites with highly customizable filtering functions.This not only allows website administrators to organize and manage content more efficiently, but also provides end users with a more intelligent and convenient content browsing experience.
Frequently Asked Questions (FAQ)
Ask: Can't all custom fields be used as filter conditions?Answer: Yes, only when the 'Whether to filter parameters' option is explicitly checked in the Anqi CMS background model management (
is_filterCustom field of the option must be selected to passarchive/filtersUsed to get through the interfacearchive/listContent filtering is performed through the interface. Fields not checked can be used for content display but cannot be used as filtering conditions.How to handle the logic of the 'All' option in front-end filter conditions?Answer:
archive/filtersEach filter condition list returned by the interface usually contains alabel: "全部"The option. In practice, when the front-end user selects "All", you just need to callarchive/listWhen interfacing, omitting the parameter for the screening condition is sufficient, the system will automatically return all content without limitations for that condition. For example, if the user selects “City: All”, then inarchive/listDo not include in the requestcityParameter.Question: If my custom field is a multi-select type, how should I pass the filter parameters?Answer: For multi-select custom fields, Anqi CMS usually supports selecting multiple values by separating them with commas as