Skip Navigation

[Resolved] Performing logic on repeating field group using wpv-for loop

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

Problem:
The customer asked how to get only the first and the last instance value from a repeating field using the "wpv-for-each" shortcode.

Solution:
Guided that the "wpv-for-each" shortcode can be used to get the first instance of the repeating field but not the last, so it will require some custom code.
https://toolset.com/forums/topic/performing-logic-on-repeating-field-group-using-wpv-for-loop/#post-2421447

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-for-each

This support ticket is created 2 years, 5 months 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by Alex Russell 2 years, 5 months ago.

Assisted by: Waqar.

Author
Posts
#2421027

Tell us what you are trying to do?

I have an RFG that contains dates. I would like to only display the first and last. Here's some badly-written pseudocode for what I am trying to do:

event-dates contains: July 1, July 2, July 3, July 5, July 10

[wpv-for-each field="wpcf-event-dates"]
"Event runs "
If index==0 [types field='event-date' style='text' format='j M, Y' item='$current_page'][/types]
" through "
IF index = last [types field='event-date' style='text' format='j M, Y' item='$current_page'][/types]
[/wpv-for-each]

So the output would be "Event runs July 1,2022 through July 10, 2022"

Is there any documentation that you are following?
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-for-each
https://toolset.com/forums/topic/conditional-on-amount-of-repeatable-fields/ -- although I would prefer to do it without a custom shortcode.

What is the link to your site?
hidden link

#2421447

Hi,

Thank you for contacting us and I'd be happy to assist.

Using the "wpv-for-each" shortcode, you can get the first instance of the repeating field like this:
( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-for-each )


[wpv-for-each field="wpcf-event-dates" start="1" end="1"]
[types field='event-dates' style='text' format='j M, Y' item='$current_page'][/types]
[/wpv-for-each]

However, it can't be used for getting only the last instance as there is no parameter available for that.

For this reason, you'll need a custom shortcode, that can return the count of repeating field items. Here is a slightly updated code, from Minesh's reply:
( ref: https://toolset.com/forums/topic/conditional-on-amount-of-repeatable-fields/#post-1389441 )


add_shortcode('count_rf_entries', 'func_count_rf_entries');
function func_count_rf_entries($atts, $content){
    $atts = shortcode_atts( array(
        'field' => '',
        'post_id' => get_the_ID(),
    ), $atts );
       
    $field = get_post_meta($atts['post_id'], "wpcf-".$atts['field'], false);
    $res = 0;
    if(is_array($field)){
        $res = count($field)-1;
    }
    return $res;
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, please add "count_rf_entries" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

After that, you'll be able to use this shortcode inside the required format of the statement like this:


Event runs
[types field='event-dates' style='text' format='j M, Y' item='$current_page' index='0'][/types]
through
[types field='event-dates' style='text' format='j M, Y' item='$current_page' index='[count_rf_entries field='event-dates']'][/types]

Note: the number that 'count_rf_entries' custom shortcode returns, is 1 less than the actual count of repeating field items because the "index" attribute in the "types" shortcode supports zero-based index (i.e. it starts from '0' and not '1').
( ref: https://toolset.com/documentation/customizing-sites-using-php/functions/#date )

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2421849

My issue is resolved now. Thank you!