I have a CPT with a over two hundred artist records, including a custom field for Last Name. My client wants these artists grouped onto three pages with last names grouped alphabetically by A-L, M-R, and S-Z , and I need help in setting up filtering for three Views accordingly.
Hello and thank you for contacting the Toolset support.
This can't be done with Toolset or WP_Query without some custom code. Because the WP_Query does not offer a "starts with" operator. It offers a "LIKE" operator, but it will need custom code to change the SQL query and remove the first % from it. Check these online threads:
- https://stackoverflow.com/a/10104988
- https://wordpress.stackexchange.com/a/346733
However, I would like to suggest a different approach. Why not use a custom taxonomy for it. The terms of the taxonomy will be the letters, and you can add a custom hook to sync the taxonomy based on the custom field value. Check a similar case, where my colleague Christian has suggested using another custom field. However, queries based on taxonomies are faster than queries built with a custom field. In addition, a custom taxonomy will provide more features, such as the count of posts, cloud tags, etc.
- https://toolset.com/forums/topic/how-to-filter-by-the-first-character-of-a-custom-field/
I hope this answers your question. Let me know if you have any questions.
I think the problem with the taxonomy approach is problematic if it only addresses the first character of the field involved.
What I would really like to be able to do is apply two filter conditions to the same field, as is possible for example with Date fields, but apparently not with other field types. In the absence of such functionality, I'm thinking I should create a duplicate of the field I want to filter and then apply the second condition to that...
A custom taxonomy will be faster than a second custom field. Honestly, I don't see a case where a custom taxonomy is not a good solution.
A second field is also a viable solution. Please check Christian suggestion from this similar thread https://toolset.com/forums/topic/how-to-filter-by-the-first-character-of-a-custom-field/
Let me know if you have any further questions.
Why is is possible to have two filter conditions for Date fields (i.e. greater than date1 AND less than date2) but not for a string field?