Problem: I would like to allow my users to filter a View by the first letter of a custom field. For example, I want the user to be able to filter with "R" and show all results where the custom field value begins with "R".
Solution: There's no way to filter by the nth character in a field value. The best way to do this is to use two separate custom fields. The first custom field will contain the complete information, and the second custom field will contain only the first character of that information. You can choose to manually add this single character during the post creation workflow, or you can choose to write some PHP code that will handle it automatically upon saving a post.
Then, filter your View by the single-letter custom field to achieve the desired filter results.
Client's example PHP code to set this single character field automatically when a post is saved:
add_action('save_post', 'schreib_alf_v_sortname_upd', 100); function schreib_alf_v_sortname_upd($id){ // only do this for reports post type if (get_post_type($id) != 'reports') return; // get the lowercase first letter of the full field value $a = lcfirst(substr(get_post_meta($id, 'wpcf-sortname', true), 0, 1)); // set the correct value of the single character field update_post_meta($id, 'wpcf-alf', $a); }
Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
https://codex.wordpress.org/Function_Reference/update_post_meta
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 5 replies, has 2 voices.
Last updated by 7 years, 4 months ago.
Assisted by: Christian Cox.