Getting Field values

It’s easy to get the values stored by Simple Fields. Most commonly used are these functions:

  • simple_fields_value()
    retrieve one or several fields values from a post
  • simple_fields_values()
    retrieve one or several repeatable field values from a post
  • simple_fields_fieldgroup()
    The easiest way to get all field values from a field group. Depending on if the field group is repeatable or not, simple_fields_value or simple_fields_values will be used.

Read more about them on their respective page, or read the quick overview below.

Gettings values from non-repeatable fields

Use the function simple_fields_value() to get value(s) from repeatable fields.

The function can take 3 arguments:

  • the field slug(s) to get the value for
  • the post to get the values from (optional, defaults to the current post in the loop)
  • options to send to field type (as array or query string).

If you have a simple text field with the slug “myFieldSlug” you can get the value from that field like this:

$field_value = simple_fields_value("myFieldSlug");
echo "The field has the value: $field_value";

You can also get the values from several fields at once: just add all the fields to the same function as a comma separated list:

$field_values = simple_fields_value("myFieldSlug,anotherFieldSlug,aThirdFieldSlug");
echo "The first field has the value:" . $field_values["myFieldSlug"];
echo "The second field has the value:" . $field_values["anotherFieldSlug"];
echo "The third field has the value:" . $field_values["aThirdFieldSlug"];

The difference between getting one field and several fields is that if you get only one field its return value gets returned directly, while getting several fields means that you get an array in return instead, with each field stored in a key with it’s slug as the key.

Getting values from repeatable fields

Use the function simple_fields_values() to get values from a field that belongs to a repeatable field group. This function uses the same arguments as somple_fields_value(). The only difference is the return value that always is an array.

$repeatable_field_values = simple_fields_values("slug_image,slug_description");
foreach ($repeatable_field_values as $values) {
echo "image has id: " . $values["slug_image"] . " and description " . $values["slug_description"]

Getting values from a whole group of fields

With simple_fields_fieldgroup you can get all values from all fields in a fieldgroup, no matter if the field group is repeatable or not.

14 thoughts on “Getting Field values

  1. Charlieman

    Hi, great plugin but it’s not recognising short codes when it pulls data from the HTML-editor textarea. Is there something I can do to fix this?

  2. Amy Davis

    The site I’m working on doesn’t give me the option to choose a slug using Simple Fields and the Show Custom Field Keys gives me the key but not the slug. I thought I saw a slug option once but can’t find it again for the life of me.

    1. Pär Thernström Post author

      When you define/add the fields you also enter the slug of each field. If you already have added slugs but forgot what you called them you can view them on the post edit screen using the “Show custom field keys”-link.

Leave a Reply