Foreach get_field_object for ACF

I have a custom field on wordpress that defines a select option

one : First
two : Second
three : Third
four : Fourth

I am trying to call these options on the frontend in a select form i.e.

Read More
<select id="filters">
  <option value="*">show all</option>
  <option value="one">First</option>
  <option value="two">Two</option>
  <option value="three">Third</option>
  <option value="four"Fourth</option>
</select>

This is what i got so far and it doesnt seem to be working:

<?php 
$field_name = "team_category";
$field = get_field_object($field_name);

foreach($field){
  echo $field['label'] . ': ' . $field['value'];
}
?>

Related posts

Leave a Reply

3 comments

  1. The ACF documentation says get_field_object() can have up to three parameters, while the second one is the Post ID. If no Post ID is used, it will use the current one, which you also retrieve with get_the_ID().

    But probably, you want these options on another page (for example, a search page, you develop). So lets say, you create a page template. What you want to search are posts with the post_type ‘teams’. So, the post_type ‘page’ has no custom fields called “team_category” and with the current post ID no object will be returned.

    What you need is a Post ID of a post which actually contains the searched field. And this should work dynamically.

    Lets try

    <?php
    global $wpdb;
    $field_name = "team_category";
    $sql = $wpdb->prepare( "select post_id from " . $wpdb->prefix . "postmeta where meta_key = %s limit 0,1 ", $field_name);
    $post = $wpdb->get_results( $sql );
    
    $field = get_field_object( $field_name, $post[0]->post_id );
    
    if( $field )
    {
        echo '<select name="' . $field['key'] . '">';
            foreach( $field['choices'] as $k => $v )
            {
                echo '<option value="' . $k . '">' . $v . '</option>';
            }
        echo '</select>';
    }
    ?>
    
  2. I think this might be something closer to what you’re looking for.

        foreach($field as $field_item){
        echo '<option value="'.$field_item['value'].'">' . $field_item['label'] . ': '</option>;
        }
    ?>
    
  3. I know this is an old question, but wanted to throw out my answer for others who may find this. There are a couple problems with your code:

    1. According to the ACF docs for get_field_object(), they say that “In some circumstances it may be necessary to load a field by its key, such as when a value has not yet been saved.” I have found that the field name does not work when trying to retrieve a field object outside of a loop (which is what you’re doing here with trying to show all options within a field, not just those related to a specific post).

      Another challenge is that the field key can differ across environments (local, staging, production, etc) unless you sync your databases. So here’s a function that can help you find the field key by field name anytime you need to use it in a function such as get_field_object():

    if (! function_exists('acf_field_from_name')) {
    
       function acf_field_from_name($field, $group)
       {
           global $wpdb;
    
           return $wpdb->get_var($wpdb->prepare("
               SELECT post.post_name as field_name
               FROM $wpdb->posts AS post
               LEFT JOIN $wpdb->posts AS parent
                   ON post.post_parent = parent.id
               WHERE post.post_excerpt = %s
                   AND post.post_type = 'acf-field'
                   AND parent.post_excerpt = %s
                   AND parent.post_type = 'acf-field-group'
           ", $field, $group));
       }
    }
    

    Will return the field key from name and group, or null if none exists.

    Usage:

    acf_field_from_name('title', 'movie-fields'); // returns field_3333333333333
    
    acf_field_from_name('title', 'book-fields'); // returns field_4444444444444
    
    acf_field_from_name('plumbus', 'movie'); // returns null
    

    See full answer by @Chris about why the group name is important in How to get Advanced Custom Fields field key from WordPress database?

    Then your code would be:

    $field_name = "team_category";
    $group_name = "team_fields";
    $field_key = acf_field_from_name($field_name, $group_name);
    $team_category_field = get_field_object($field_key);
    
    1. The second problem is the options for select, checkbox, and radio fields are stored as an array inside an item within the field object array under the key choices. So to get those you need to use:
      $team_category_choices = $team_category_field['choices'];
    
    1. The third problem is your foreach() function is incorrect. You need to pass the array and define how each of the array item keys (choice value saved to DB) and values (choice label shown in UI) in it will be referenced within the inner function, like so:
    foreach($team_category_choices as $choice_value => $choice_label) {
      echo '<option value="' . $choice_value . '">' . $choice_label . '</option>';
    }