• Resolved Jason Judge

    (@judgej)


    I am using a validation list to give me a drop-down list of allowed values for a custom field. The list includes values and labels, the labels being what is stored.

    Now, I would like to display the labels on a page, given the stored value. Is there an API I can use to get hold of the value/label pairs for the field, so I can do the mapping for display?

    — Jason

    http://wordpress.org/extend/plugins/advanced-custom-fields/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi

    Check out this answer I gave on the support section of acf.
    http://support.advancedcustomfields.com/discussion/comment/9283#Comment_9283

    You should be able to get what you want using get_field_object.

    Thread Starter Jason Judge

    (@judgej)

    That’s great – thanks. Your example uses this to get the custom field opject:

    $field_obj = get_field_object( 'field_506d7b9428874', $post_id );

    The “field_506d7b9428874”, where would that come from? I’m assuming it is the custom field ID, but I cannot see that in the ACF admin screens since it is all AJAX and these codes don’t appear in any URLs. Do I need to look at the page HTML source to get that? Or can I just use the “field name” slug as it appears in the admin screen?

    — Jason

    You can view this field key in the admin area by going into Custom Fields > your field group > Screen Options (top right) > and set “Show Field Key” to Yes

    See the pic for clarification
    http://tinypic.com/r/rk7bsh/6

    This will show you the field key next to your custom fields.

    BTW,
    You can also see these keys in your MySQL db if you search for the field name in the postmeta table.

    Thread Starter Jason Judge

    (@judgej)

    Brilliant! You’re a star 🙂 I seldom think of looking in that “screen options” drop-down panel, and forget it contains contextual display options.

    I will be using this to expand the grade codes of second-hand disks in this shop:

    http://www.soulbrother.com/shop/turn-you-to-love/

    Just a simple code is stored (M and M in that example), but I would like to display the full description of those codes, which is defined in the ACF field.

    I’ll post the code I use, then close this ticket.

    Had a similar problem.

    Glad I could help.

    Thread Starter Jason Judge

    (@judgej)

    Here we go – works a dream. In the WooCommerce meta.php template, I check if the get_field_object() function exists (because it won’t if ACF is not enabled), then fetch the two lists:

    if (function_exists('get_field_object')) {
                $field_obj = get_field_object( 'field_5062e50c7da52', $product->id );
                if (!empty($field_obj['choices'])) $disc_grade_choices = $field_obj['choices'];
    
                $field_obj = get_field_object( 'field_5062e62519095', $product->id );
                if (!empty($field_obj['choices'])) $sleeve_grade_choices = $field_obj['choices'];
            }

    Further down, after I fetch the custom fields into $disc_grade and $sleeve_grade, I check if those grades are an index value in the lists, then switch in the description if they are:

    if (!empty($disc_grade_choices) && !empty($disc_grade_choices[$disc_grade])) $disc_grade = $disc_grade_choices[$disc_grade];

    Always erring in the side of safety, of course, because you cannot guarantee a given plugin or theme is going to be there.

    The code isn’t rocket science, I know, but may be useful to someone. Thanks again.

    — Jason

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Getting the list of validation options and labels’ is closed to new replies.