I am trying to: Show/include only future dates from a multiple instance custom field "Date' within a View. [types field="workshop-data-start" style="text" format="d/m/Y" separator="<br/>"][/types]. So a for-each and if-evaluate needs to show the Dates that are > (greater than) the current date/time. I believe the evaluate needs to perform a comparison based on timestamp value ( time() ).
I visited this URL: https://toolset.com/forums/topic/date-field-multiple-instances-show-only-today-or-higher/
I expected to see: The above topic mentions some issues with processing code in a certain order, so using this code example doesn't do the job.
Instead, I got: Tried some alternatives, but don't seem to get any results. The View displays properly but the for-each and if-evaluate returns 'empty'. In the attached screenshot is the View displayed. There is a date shown (12/03/2015) in the second row. This date shouldn't be shown based on the current date of 18/03/2015.
The problem is the for-each statement, which doesn't seems to execute together with the if statement.
Below code works partially. When the dates are all in the future, they show. When there is a date in the Date instances that is in the past, the for-each stops working.
[wpv-for-each field="wpcf-workshop-data-start"]
[wpv-if expiration="wpcf-workshop-data-start" evaluate="$expiration > '[today_date]'"]
[types field="workshop-data-start" style="text" format="d/m/Y"][/types]
[/wpv-if]
[/wpv-for-each]
Ok, i see at this URL: https://toolset.com/documentation/views-shortcodes/#wpv-for-each
Note: wpv-if shortcode won’t work inside a wpv-for-each shortcode
So, it isn't supposed to work anyway in the current code.
Is there a work-around or is it just not possible?
Would be great if this could be added to the existing code. Let's you create a lot of additional/conditional Views with multiple instance values.
Yes, you are right, as the document mentioned wpv-if shortcode won’t work inside a wpv-for-each shortcode
https://toolset.com/documentation/views-shortcodes/#wpv-for-each
In your case, I suggest you try create a custom shortcode for it, for example:
1) add below codes in your theme functions.php:
add_shortcode('my-repeating-dates', 'my_repeating_dates_func');
function my_repeating_dates_func($atts, $content){
extract( shortcode_atts( array(
'field' => '',
'strtotime' => 'Today',
'style' => 'text',
'format' => 'd/m/Y',
'separator' => ', ',
), $atts ));
$res_arr = array();
$arr = get_post_meta(get_the_ID(), $field, false);
$value = strtotime($strtotime);
foreach($arr as $k=> $v){
if($v > $value){
$res_arr[] = date_i18n( $format, $v);
}
}
$res = implode($separator, $res_arr);
return $res;
}
2) use below above shortcodes in your content, like this:
[my-repeating-dates field="wpcf-workshop-data-start" strtotime="Today" style="text" format="d/m/Y" separator=", "]
Luoy,
Works like a charm. Would be cool if there could be a bunch of ready-made functions like this available which you can use out-of-the-box.
This code snippet is a great starting point for solving more things like this.
Thank you very much for support!
Marcellino