I can't find any tutorial or good examples for filtering posts using custom fields.
I have created Products CPT and the following custom fields:
Is New?
Is Popular?
Is Discontinued?
These are individual checkboxes.
I then created an archive page and in WordPress Archive > Loop Section > Query Filters > Add Filter, I selected "Is Discontinued?" custom field but unable to decide if it would be a string, number or boolean. And then what values to specific when using checkboxes.
I went through the documentation, but couldn't find any detailed explanations or examples. It would be much appreciated if all combination of examples are provides for all the field types, operators & values.
Hello, I do not have any online documentation links available exactly like you have requested, but in general when filtering by custom field you can use the default option 'as a string' in most cases. The main exceptions are for Toolset's numeric fields, which should be compared as a "number" for best results, and for Toolset's date field type. Toolset date fields store information in Unix timestamp format, and should be compared as an "UNSIGNED" or as a "number" for best results.
The specific value you should use in the Query Filter depends on the field settings. By default, a checkbox is set to store the value 1 when checked, but it is possible to change that value. You would have to verify the settings in the custom field editor by going to Toolset > Custom Fields > Post Fields, then edit the field group containing the checkbox, and edit the checkbox field itself. You can find the exact value to use in "Value to store" (screenshot attached).
For a date field, you would specify a date by Unix timestamp value. You can convert any human readable date to timestamp using online tools like the Epoch Converter https://epochconverter.com
Let me know if you have a specific question about some other field type or data type and I will try to find the best answer for you.
I am getting this now.
I tried "Is Discontinued?" is a number different from 1, and it worked. Earlier, it was not filtering out the products. Either it used to show no products or all the products.
Maybe you can inform the concerned person to update the documentation to include more examples and explanations. It would save everyone's time.
Good point, I will file a request with our documentation team to update the documentation for the Query Filter options. Thanks for the report.
My issue is resolved now. Thank you!