Support

Account

Home Forums Add-ons Repeater Field get_sub_field outside (have_rows)

Solved

get_sub_field outside (have_rows)

    • topi

    • September 25, 2017 at 5:55 pm

    Hello, my repeater is located in profile page
    ‘great_repeater’->
    –‘sub_field_1’
    –‘sub_field_2’

    $id = userid
    $key = correct row
    1 = value (for true/false -field)

    update_sub_field(array(‘great_repeater’, $key, ‘sub_field_1’), 1, ‘user_’ . $id); //THIS WORKS JUST FINE BUT…

    I need to check the value(1) before so how can I do it with get_sub_field?

    My final goal is:

    if (get_sub_field(something..something..wuts?) == 0) {
    update_sub_field(array(‘great_repeater’, $key, ‘sub_field_1’), 1, ‘user_’ . $id);
    } else {
    update_sub_field(array(‘great_repeater’, $key, ‘sub_field_1’), 0, ‘user_’ . $id);
    }

    • psics

    • September 25, 2017 at 11:15 pm
    <?php if( get_field('great_repeater') ): ?>
    
    	<?php while( has_sub_field('great_repeater') ): ?>
    
    		<?php 
    
    		$select = get_sub_field('sub_field_1');
    
    		?>
    
    			<?php foreach( $select['choices'] as $k => $v ): ?>
    				ifelse
    			<?php endforeach; ?>
    
    	<?php endwhile; ?>
    
    <?php endif; ?>
  • You can’t use get_sub_field() outside of a have_rows() loop.

    You can get the value of that sub field directly using get_post_meta().

    For example, if your repeater is named “repeater” and the sub field you want to check is named “sub_field” and the row you want to check is the 2nd row then

    
    $value = get_post_meta($post_id, 'repeater_1_sub_field', true);
    

    indexes start a 0 so the second rows index is 1.

    This is how repeaters and sub fields are named

    
    "{$repeater_name}_{$index}_{$sub_field_name}"
    
    • vasko_t

    • March 28, 2018 at 4:34 am

    @hube2 I think you have a typo in your answer. It should be:

    $value = get_post_meta($post_id, 'repeater_1_sub_field', true);

    .. that is, a comma after the $post_id instead of a period.

    Cheers!

  • well spotted, I’ve updated my comment to correct

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.