Skip Navigation

[Resolved] Sorting and styling dates from a repeating custom field

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.

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

Problem: I have a repeating "date" type custom field. I would like to sort the dates in chronological order, and I would like to apply a strike-through style to the dates that occurred in the past.

Solution:
Add this to functions.php:

add_shortcode( 'sort_and_format_post_date_fields', 'sort_and_format_post_date_fields_func');
function sort_and_format_post_date_fields_func($atts = [])
{
  $atts = shortcode_atts([
    'postid' => '',
    'field' => '',
    'format' => 'l F jS Y',
  ], $atts);
  $values = get_post_meta($atts['postid'], 'wpcf-my-date');
  usort($values, 'numbersort');
  $string = '';
  foreach($values as $value) {
    $date = date_i18n($atts['format'], $value);
    $prefix = $value < strtotime('0:00:00') ? '<span style="text-decoration:line-through;">' : '';
    $suffix = $value < strtotime('0:00:00') ? '</span>' : '';
    $string .= ($string == '' ? '' : ', ') . $prefix . $date . $suffix;
  }
 
  return $string;
}
 
function numbersort($a, $b)
{
  $res = 0;
  if ( $a > $b) {
     $res = 1;
  }
  return $res;
}

Then use it by placing this in the content of the post that includes the date field:

[sort_and_format_post_date_fields postid="[wpv-post-id]" field="my-date" format="l F jS Y"]

Relevant Documentation:
https://toolset.com/documentation/user-guides/repeating-fields/
https://codex.wordpress.org/Shortcode_API
http://php.net/manual/en/function.date.php
https://codex.wordpress.org/Function_Reference/date_i18n

This support ticket is created 7 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

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 8 replies, has 2 voices.

Last updated by toolset-dave 7 years ago.

Assisted by: Christian Cox.

Author
Posts
#583651

Hi,

can you show me how to sort multiple dates from the oldest to the most future ones and if the date is in previous date than now it will be decoraded with line-through?

[types field='my-date' style='text' format='j. F Y %o%d G:i' separator='<br>'][/types]

#584348

Is the my-date field set up to allow multiple values, or is it a single value field on multiple posts? If it's a single field on multiple posts, then you can create a View of posts, sorted by your custom date field, in ascending order. Then in your Loop Output you can use a conditional that tests the value of the my-date field, and applies a line-through style using CSS:

<span [wpv-conditional if='( $(wpcf-my-date) lt "TODAY()" )']style="text-decoration:line-through"[/wpv-conditional]>[types field='my-date' style='text' format='j. F Y %o%d G:i'][/types]</span>

Let me know if I've misunderstood your request, and I'll take another look.
- edit - updated date format to match your original post.

#584613

Hi, my-date field is allowed to have multiple values in single post. Sorry for not mentioning it before. Btw I will use this code for multiple posts too, thanks.

#584849

Types does not offer a method for sorting those fields within a single post, unfortunately. You can use the wpv-for-each shortcode to loop over each field and apply the line-through styles like I was describing earlier, but sorting the display of those field values would require custom code. More information about displaying repeating fields here:
https://toolset.com/documentation/user-guides/repeating-fields/

#584907

I tried to combine the codes, but the result not work for me:

[wpv-for-each field="wpcf-my-date"]
<span [wpv-conditional if='( $(wpcf-my-date) lt "TODAY()" )']style="text-decoration:line-through"[/wpv-conditional]>[types field='my-date' style='text' format='j. m Y %o%d G:i'][/types]</span><br>
[/wpv-for-each]

Each date is decorated with the style even if it is in future.

Can you help me to create custom shortcode with sorting?

#584994

Add this to functions.php:

add_shortcode( 'sort_and_format_post_date_fields', 'sort_and_format_post_date_fields_func');
function sort_and_format_post_date_fields_func($atts = [])
{
  $atts = shortcode_atts([
    'postid' => '',
    'field' => '',
    'format' => 'l F jS Y',
  ], $atts);
  $values = get_post_meta($atts['postid'], 'wpcf-my-date');
  usort($values, 'numbersort');
  $string = '';
  foreach($values as $value) {
    $date = date($atts['format'], $value);
    $prefix = $value < strtotime('0:00:00') ? '<span style="text-decoration:line-through;">' : '';
    $suffix = $value < strtotime('0:00:00') ? '</span>' : '';
    $string .= ($string == '' ? '' : ', ') . $prefix . $date . $suffix;
  }

  return $string;
}

function numbersort($a, $b)
{
  $res = 0;
  if ( $a > $b) {
     $res = 1;
  }
  return $res;
}

Then use it by placing this in the content of the post that includes the date field:

[sort_and_format_post_date_fields postid="[wpv-post-id]" field="my-date" format="l F jS Y"]
#585008
month-display.png

Hi Christian,

thank you so much for the code, it´s great! I have made some cosmetic edits. But there is one think I can´t solve. The formating of date is the same as I use on the web, but the month ("F") is displaying in English instead of my Czech language, which is set as default in WordPress.

#585575

Okay I see, try updating the code at line 13 to use the date_i18n function instead of the date function:

$date = date_i18n($atts['format'], $value);
#585578

Works perfectly. Thank you, Christian.

The forum ‘Types Community Support’ is closed to new topics and replies.