Support

Account

Home Forums Add-ons Flexible Content Field get_field returns null for repeatable field in flexible content field

Solved

get_field returns null for repeatable field in flexible content field

  • Hi!

    My structure is as follows

    • field name blocks (flexible field)
      • layout name puff (repeatable field named puff with three puffs in it)

    I then have a blocks template that gets all the layouts and includes a template for the specific layout

    
    if (have_rows('blocks')) :
    
        // loop through the rows of data
        while (have_rows('blocks')) :
            the_row();
    
    		// Load block template from blocks directory
    		$layoutName = get_row_layout();
    
    		if (!$blockPath = locate_template('blocks/' . $layoutName . '.php')) {
    			trigger_error(sprintf(__('Block template %s missing', 'sbs'), $layoutName), E_USER_ERROR);
    		}
    
    		require $blockPath;
    
        endwhile;
    
    else :
        // no layouts found
    	echo '<p>No layouts specified</p>';
    
    endif;
    

    then in the puff.php file

    
    <div class="row">
    	<?php
    		$puffs = get_field('puff'); // this returns NULL
    		$puffCount = count($puffs);
    
    		function puffColumns($count) {
    			switch ($count) {
    				case 1:
    					return 'col-xs-12';
    				case 2:
    					return 'col-xs-12 col-md-6';
    				case 3:
    					return 'col-md-6 col-lg-4';
    				case 4:
    					return 'col-md-6 col-lg-3';
    			}
    		}
    
    		while (have_rows('puff')) : the_row();
    	?>
    
    		<div class="<?php echo puffColumns($puffCount); ?>" style="background-image: url('<?php the_sub_field('bg'); ?>')">
    			<?php the_sub_field('title'); ?>
    		</div>
    
    	<?php endwhile; ?>
    </div>
    

    This works great and it loops out all the puffs.
    the problem is that when I try to get the repeatable field so that I can do a count and echo the different column layouts based on the number of items in the repeatable field, It always returns null.

    Am I doing something wrong?

    Can it be that i named the layout puff and also the repeatable field inside it puff?

    According to other forum posts doing count(get_field(‘puff’)) to get the count is the way to go, is this still the best way to go?

  • Use get_sub_field(), the repeater is a sub field of the flex field

    $puffs = get_sub_field('puff');

  • Don’t know why I didn’t try that…

    Thanks for the help 🙂

  • It’s an easy thing to do, especially when you’re repeater loop is in a different template than the flex loop. Glad I could help.

  • I just ran into this same issue with the Flexible Content layouts and I am still getting an error on get_sub_field.

    The only difference is get_field() returns NULL and get_sub_field() returns bool(false)

    The odd thing is that the rest of the loop works just fine with while( have_rows() ) and I can loop through this and retrieve my data.

    My layout is called Testimonials and the name is the same. My repeater in this layout is called has a field name of testimonials_module.

    Any Ideas?

  • get_field() should return null because the field is a sub_field and there is not top level field with that name.

    As far as get_sub_field() returning false…. what type of field is it and what version of PHP is running on the server?

  • PHP7 is running on the server and the field is a repeater. I’m trying to get a count of the repeater items with $count = count( get_sub_field( 'testimonials_module' ) );. I’ve even tried this with get_the_ID() as the second param to force this but that did not work either. A dump of get_the_ID() is pulling the correct ID by the way.

  • Just 7? Is that < 7.1.0 or >= 7.1.0,

    although if your trying to get the count of a repeater then is should be returning an number.

    get_sub_field() does not take a post ID argument. This may sound like a stupid question, but are you sure it’s a repeater?

    The reason I’m asking these questions is that the only reason that this should be getting a false value is that there is actually a value in the database and for some reason it’s being interpreted as false rather than whatever is store in the db.

  • I should also note that I am using get_template_part if get_row_layout matches a string. And I just tried moving this echo count( get_sub_field('testimonials_module' ) ); outside of the get_template_part and it works!

    I think I just found the issue but it is still super confusing. At the top of my template part I have this:

    if ( ! have_rows( 'testimonials_module' ) ) {
    	return;
    }

    If I call echo count( get_sub_field('testimonials_module' ) ); before this conditional it works fine but after it, it returns false. How would this effect the return of get_sub_field when that conditional is returning false?

    PHP version is 7.0.7. Valid question, but yet I am sure it is a repeater since the while( have_rows() ) loop is working and actually returning my data.

    Weird, right?! Thanks for responding.

  • The reason that I was so curious about the version of PHP is that there is a change in PHP7.1 that effects how unserialze works and there was another recent topic where this was the cause.

    Your issue is different and has to do with acf loosing track of what loop it’s in when using get_template_part()…. the is a topic here on the forums someplace that discusses this and I’m pretty sure that it was solved, but it’s been a long time, I’d say it was a year ago. I’m looking for it but not having much luck.

  • thought of another question to ask.

    Are you doing a custom WP_Query?

    
      $query = new WP_Query($whatever);
      while($query->have_posts())) {
        // somewhere in here you're calling get_template_part
      }
    

    Or are you in some way looping over posts that is a nested loop inside of the main WP loop?

  • Good question. No custom query happening. I even tried doing a wp_reset_postdata() at the top of the template part before the ACF loop to see if that would help, but no luck.

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

You must be logged in to reply to this topic.