Home Forums ACF PRO get_sub_field_object select labels


get_sub_field_object select labels

  • I’m finding that since ACF PRO, get_sub_field_object will no longer return the labels of a ‘select field’ using the code below:

    if (have_rows('example') ) : while(have_rows('example') ): the_row();
    $object = (get_sub_field_object('field_object'));									
    $value = get_sub_field('field_object');									
    $label = ($object['choices'][$value]);
    <li class="<?php $value ?>"><a><?php echo $label; ?></a></li>

    This was working previously before the ACF PRO upgrade. Re-saving or re-creating the custom field doesn’t seem to correct this issue.


  • Hi @robbie

    Thanks for the bug report. Are you able to debug the field object data like so and report back with the findings?

    echo '<pre>';
    echo '</pre>';
  • When I print_r($object) from the above example, nothing at all is displayed. It’s like the object doesn’t return anything. So the result is:


    However, I noticed I have another of these loops where get_sub_field_object function appears to beworking, but it’s not in the have rows function.

    if (get_sub_field('class_title') ) :
    $title_select = (get_sub_field_object('class_title'));
    $title_class = get_sub_field('class_title');
    $title = ($title_select['choices'][$title_class]);
    echo '<h2 class="' . $title_class . ' desktop"><a href="' . $title_class . '">' . $title . '</a></h2>';
    echo '<h2 class="' . $title_class . ' mobile">' . $title . '</h2>';

    This is displaying the correct values and labels. print_r(title_select); gives the full array.

  • Is there any other information I can provide? Is it wise/acceptable to use the get_sub_field instead of the have rows?

  • This reply has been marked as private.
  • I have reverted back to 4.3.8 for now, using the method above the labels are output properly. I think I will have to wait before upgrading.

  • Hi @robbie

    Thanks for the field group code.

    Your original code: $object = (get_sub_field_object('field_object'));

    However, your field group does not contain a field with a key or a name of ‘field_object’…

    perhaps you are misusing the parameter?

  • Hi Elliot, sorry i mixed around my responses and was providing a non-specific example. With regards to the field group code, here is the code i’m using for this:

    if (have_rows('class_menu') ) : ?>
    <ul class="class-menu">
    <?php while(have_rows('class_menu') ): the_row();
    $class_select = (get_sub_field_object('menu_class'));
    $class = get_sub_field('menu_class');
    $label = ($class_select['choices'][$class]);
    $link = get_sub_field('menu_link'); ?>
    <li class="<?php echo $class; ?>"><a href="<?php echo $link; ?>"><?php echo $label; ?></a></li>
    <?php endwhile; ?>

    It might get a little confusing, but this works when reverted back to 4.3.8 – is it possible that the database upgrade didn’t work properly? I’m working on a localhost if that helps.

  • Hi @robbie

    Thanks for the info. I’m interested to use your above field group code to test the issue myself, but can’t copy / paste the code due to all quotes being converted to html characters.

    Are you able to edit your comment and re-post your code within backticks to prevent the html entity issue?


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

    Thanks for the field group, it has imported great!

    I just noticed an issue with your code. Your flexible content field contains 2 layouts, however, your code does not contain any reference to the function get_row_layout().

    Perhaps the sub field code is not working, because the sub field does not actually exist for that row?

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

    Please ignore my previous comment, I now see you had only included the inner loop. After adding in the outer loop, I can replicate the issue!

    I have now fixed the issue and the function get_sub_field_object will now work with nested sub fields!

    The fix is in api-template.php line 627:

    $f = acf_get_sub_field( $field_name, $row['field'] );

    That should fix the issue for now until the next version is out!

  • Champion! That fixed the issue! Thanks! Sorry for all the replies – we got there eventually! If there are any further issues I will post entire code blocks next time!

    One quick question, is your a custom built area? Is it woocommerce with a custom theme?

    Thanks again.

  • Hi @robbie

    Cheers mate. Yep, it’s woo-commerce with a custom theme

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

The topic ‘get_sub_field_object select labels’ is closed to new replies.