Problem: I have a repeating field that includes simple text strings. I would like to display the values of that field in alphabetical order.
Solution: It could be complicated if the field accepts HTML, or if special characters or complex fields are considered. If you just want a basic text comparison on a simple Types field, you can add this custom shortcode to your child theme's functions.php file or create a new snippet in Toolset > Settings > Custom Code:
add_shortcode( 'ts-sort-multi-field-values', 'ts_sort_multi_field_values_func'); function ts_sort_multi_field_values_func( $atts, $content ) { global $post; $a = shortcode_atts( array( 'direction' => 'ASC', 'slug' => '', 'glue' => ', ', ), $atts ); $vals = get_post_meta($post->ID, 'wpcf-' . $a['slug']); if($a['direction'] == 'ASC') usort($vals, "alpha_vals_asc_cmp"); if($a['direction'] == 'DESC') usort($vals, "alpha_vals_desc_cmp"); return implode($a['glue'], $vals); } function alpha_vals_asc_cmp($a, $b) { $stra = strtolower($a); $strb = strtolower($b); $res = 0; if ( $stra < $strb) { $res = 1; } return $res; } function alpha_vals_desc_cmp($a, $b) { $stra = strtolower($a); $strb = strtolower($b); $res = 0; if ( $stra > $strb) { $res = 1; } return $res; }
Then use the shortcode like this to display a sorted list of the field values:
[ts-sort-multi-field-values slug='your-field-slug' direction='ASC' glue=', ']
Replace your-field-slug with the slug of your custom field. Direction can be ASC or DESC, and glue is the separator between values.
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 4 replies, has 2 voices.
Last updated by 6 years, 1 month ago.
Assisted by: Christian Cox.