Skip Navigation

[Resolved] Display sorted repeating field values

This thread is resolved. Here is a description of the problem and solution.

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 support ticket is created 6 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

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 MC 6 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1143122

MC

Hello,

If i have a field with multiple instances, Is it possible to sort the field values alphabetically?

thx

#1143265

Hi, that type of customization will require custom code. 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=', ']

Direction can be ASC or DESC, and glue is the separator between values.

#1143660

MC

Christian,

Thanks a lot! I will try this. And no, it is not complicated, it are just single lines of text.

I also forget to ask, can i also create of each value a link?

Thx!

#1144474

Sure, you can use the wpv-for-each shortcode to loop over each repeating item and output a link. More information about that here: https://toolset.com/documentation/user-guides/views-shortcodes/#vf-153482

#1145962

MC

My issue is resolved now. Thank you!