Support

Account

Home Forums ACF PRO Get specific values from repeater

Solved

Get specific values from repeater

  • Hi,

    I have this repeater with 3 fields:
    start date, end date and price and i have like 3-4 rows like this:
    12/12/2016 24/12/2016 $400
    24/12/2016 31/12/2016 $500
    etc

    i need to get the price for the selected dates, is it posible with an acf function or should i do it in other way?

    This is the repeater code:

    array (
    			'key' => 'field_572b858626762',
    			'label' => 'dates',
    			'name' => 'dates_repeater',
    			'type' => 'repeater',
    			'collapsed' => 'field_572b859f26763',
    			'layout' => 'table',
    			'button_label' => 'add date',
    			'sub_fields' => array (
    				array (
    					'key' => 'field_572b859f26763',
    					'label' => 'Start',
    					'name' => 'date_start',
    					'type' => 'date_picker',
    					'display_format' => 'd/m/Y',
    					'return_format' => 'Y-m-d',
    					'first_day' => 1,
    				),
    				array (
    					'key' => 'field_572b86927bb0e',
    					'label' => 'End',
    					'name' => 'date_end',
    					'type' => 'date_picker',
    					'display_format' => 'd/m/Y',
    					'return_format' => 'Y-m-d',
    					'first_day' => 1,
    				),
    				array (
    					'key' => 'field_57ab9b20dd22a',
    					'label' => 'Price',
    					'name' => 'price_by_dates',
    					'type' => 'text',
    					'instructions' => '',
    					'append' => '€',
    				),
    			),
    		),
  • The only way you can get a specific row based on at value is to loop over the repeater until you find what you’re looking for and then show it.

    
    if (have_rows('dates_repeater')) {
      while (have_rows('dates_repeater')) {
        the_row();
        $now = date('Ymd'); // this is the format stored by ACF
        // the above is also the format you need to use to compare dates
        // the second parameter of <code>false</code> tells ACF not to format
        // the value so that we can compare them
        if (get_sub_field('date_start', false) >= $now && 
            get_sub_field('date_end', false) <= $now) {
          the_sub_field('price_by_dates');
        }
      }
    }
    
  • Ok, that’s what i thought, that i had to use a loop
    Thk

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Get specific values from repeater’ is closed to new replies.