Support

Account

Home Forums ACF PRO Getting Select Field Values From Post Object Inside Repeater

Solved

Getting Select Field Values From Post Object Inside Repeater

  • Hello,

    So I know my title is confusing. So I will explain what is going on.

    I have a CPT called downloads, which has a ACF Select Field for the type of download. I only have one other ACF type for the this CTP, which is a URL field type.

    Then I have a page called “downloads” using an ACF Repeater, and the only sub-field being a Post Object, that allows you to select which Downloads you want to display.

    To help understand my code, here are my field names and what type they are:

    Downloads CPT:

    button_url – URL Field
    download_type – Select Field

    Downloads Page Template (not used as an archive or single template for individual downloads CPT’s, but a page where I can specify which downloads I want to provide):

    featured_downloads – Repeater
    download – Post Object (sub field of featured_downloads)

    Here is my code:

    
    <?php
    
    if( have_rows('featured_downloads') ) : 
    	while( have_rows('featured_downloads') ) : the_row(); 
    
    		$download_object = get_sub_field('download');
    
    		if( $download_object ) :
    			$post = $download_object;
    			setup_postdata( $post );
    
    			$type = get_field_object( 'download_type' );
    			$value = $type['value'];
    			$label = $type['choices'][ $value ];
    			$url = get_field( 'button_url' );
    	?>      	
    
    	<div class="featured-download row">
    		<div class="medium-5 columns text-center">
    		<?php the_post_thumbnail( 'full' ); ?>
    		</div>
    		<div class="medium-7 columns">
    			<div class="download-type"><?php echo $label; ?></div>
    			<h3><?php the_title(); ?></h3>
    			<a href="<?php echo $url; ?>" class="button">Download Now</a>
    		</div>
    	</div>
    <?php wp_reset_postdata(); endif; endwhile; endif; ?>
    

    I’m able to display exactly what I need except the $label variable that stores the label for the select value. I get this error message:

    Warning: Illegal offset type in …(the path to the file the above code is on) on line 12.

    Line 12 in my code is the setting of the $label variable.

    Any help would be greatly appreciated

  • Hi @kiwicreative

    Could you please share the JSON export file of your field group so I can test it out on my installation?

    Thanks 🙂

  • This reply has been marked as private.
  • Hi @kiwicreative

    Thanks for sharing the JSON file.

    I’ve just checked it and found out that the Return Format of the download_type field is set to “Both (Array)”. In this case, you don’t need to use the get_field_object() function anymore. You should be able to do it like this:

    $type = get_field( 'download_type' );
    $value = $type['value'];
    $label = $type['label'];

    I hope this helps 🙂

  • That did it!

    Thanks so much for your help, James.

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

You must be logged in to reply to this topic.