In AnQi CMS, setting up 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, thus significantly enhancing the website's user experience.Next, let's learn how to implement this feature in the Anqi CMS.
Custom field: The 'alive' tag of the content
Firstly, we need to understand what custom fields are.In AnQi CMS, custom fields are additional data items you add for specific content models (such as articles, products, etc.).They can be text, numbers, radio buttons, checkboxes, drop-down menus, and more types.For example, if you operate a real estate website, you can add custom fields such as 'apartment layout', 'area', and 'location' to the 'house' content model; if you manage a job recruitment website, you can add fields such as 'city', 'education', and 'work experience'.
These custom fields are not only used for storing and displaying additional information, but also play a crucial role as they can become the key to filtering user content.By setting it as a filter condition, you can transform your website from a simple content display platform to a powerful information retrieval tool.
Turn on the filter function: Backend configuration is the key
Change a custom field to a filter condition, the first step is to set it in the Anqi CMS backend management interface.You need to enter the corresponding model management page where you want to configure the filter function.For example, if your article model needs to be filtered by 'author' or 'publishing region', you will enter the field management of the article model.
In the field settings of the model, you will see that each custom field has a name called “Is Filter Parameter”is_filterThe options. [en]This option is the key to the filtering function.When you check this option and save it, the Anqi CMS will recognize this custom field as a condition that can be used for content list filtering.
It is worth noting that for fields that have preset option values (such as radio buttons, checkboxes, dropdown menus), the values you enter when creating the field will automatically become specific options in the front-end 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 there are when your custom field is configured as filterable? At this point, you need to use the interface provided by Anqi CMS.archive/filtersinterface.
By sending toarchive/filtersSend a GET request through the interface, and include the filtering conditions you want to retrievemoduleId(Model ID), it will return a list containing all the fields that can be filtered. Each field will have its display name (name), the actual call name used in API calls (field_name) as well as a series of specific filtering optionsitems).
For example, if you request the filtering conditions for model ID 1, the returned data may include two filtering fields such as 'City' and 'Education', with specific options listed under each field, such as 'Beijing', 'Shanghai', 'Master', 'Bachelor'.This information can help you dynamically generate a filter form on the website frontend, where users can select predefined conditions without manual input.
Apply filter: precisely obtain the desired content
After obtaining the filtered condition list, the next step is how to apply these conditions to the actual content query, thus obtaining a more precise document list.archive/listThe interface is the core to achieve this goal.
When callingarchive/listWhen it comes to the interface, in addition to the conventionalmoduleId/categoryId/orderparameters, you can also directly use custom fieldsfield_nameAs URL query parameter names, and pass the user's selected option values as parameters.
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 "degree" as "Bachelor", you can continue to add parameters:
{域名地址}/api/archive/list?moduleId=1&city=北京&certificate=本科
Through such a combination of parameters,archive/listThe interface will return a list of content that only meets these filtering conditions, greatly enhancing the efficiency and usability of users in searching for information.
Summary
By following these steps, the flexible custom field configuration of Anqi CMS combined with powerful API interfaces makes it easy to build a website with highly customizable filtering functions.This not only enables website administrators to organize and manage content more efficiently, but also provides end-users with a more intelligent and convenient content browsing experience.
Common Questions (FAQ)
Ask: Can not all custom fields be used as filtering conditions?Answer: Yes, only when 'Whether to filter parameters' is explicitly checked in the model management of Anqi CMS background model management,
is_filter)Option custom fields, only those checked can be usedarchive/filtersto obtain and use through the interfacearchive/listto filter content through the interface. Unchecked fields can be used for content display, but cannot be used as filtering conditions.What is the logic for handling the 'All' option in the frontend filter conditions?Answer:
archive/filtersEach filter condition list in the returned data from the interface usually contains alabel: "全部"[en]The option. In practical applications, when the front-end user selects "All", you only need to callarchive/listWhen using the interface, you can omit the parameter of the filtering condition, and the system will automatically return all content without any limitations on that condition. For example, if the user selects 'City: All', then inarchive/listIt is not necessary to include in the requestcityParameter.Q: If my custom field is a multi-select type, how should I pass the filter parameters?Answer: For custom fields of multiple choice type, AnQi CMS usually supports specifying multiple selected values by separating them with commas.