Working with simple meta field object

In continuing with the objects and functions needed to get data from meta box and its meta fields, we are now on the way to actually work with meta fields and to see how we can get data from it to work with or display.

As it was discussed in previous articles, the plugin supports two types of meta fields: Simple Meta Field and Custom Meta Field. This article is for simple meta fields.

Metabox initialization and checking for values

Each field can be set as the repeater, and in theory, each field can hold no values, one value or multiple values. So, to see this, we will use the example from previous articles to show full meta box code in a loop.

$metabox = gdcet_metabox($meta_box_name);

while ($metabox->have_fields()) :
  $metabox->the_field();

  if (gdcet_the_field()->have_values()) :
    // you can work with this field data //
  endif;
endwhile;

First thing here is that we are using gdcet_the_field() function to get the current field in the loop. If you are using non-loop approach, here is the example:

$metabox = gdcet_metabox($meta_box_name);
$metafield = $metabox->field($field_name);
if ($metafield->have_values()) :
  // you can work with this field data //
endif;

The approach is the same. And, if we are inside the loop, we know that field has at least one value. To go through all the value, the plugin uses a loop on the field object, or you can manually work with every value.

Simple meta field basic methods

Here are the methods field object has, that are not part of the loop. These methods can be used if you plan to avoid using the loop. Also, if your fields have one value, you don’t need the whole loop anyway.

$metafield->have_values()

This is the base loop method, but it can be used anywhere, it returns true or false if the field has values or not. If inside the loop it will return false when the loop ends.

$metafield->count_values()

This return number of values field has.

$metafield->value($index = 0)

This will set the field with the value at selected index. Calling it without the index will set first (index 0) value into the field.

$metafield->is_simple_field()

Returns true if the field is simple meta field.

$metafield->is_custom_field()

Returns false if the field is simple meta field.

$metafield->get_label($args = array())

Returns the label for the meta field. $args can contain ‘before’ and ‘after’ arguments to wrap the label.

$metafield->label($args = array())

Print the label for the meta field. $args can contain ‘before’ and ‘after’ arguments to wrap the label.

$metafield->get_description($args = array())

Returns the description for the meta field. $args can contain ‘before’ and ‘after’ arguments to wrap the description.

$metafield->description($args = array())

Print the description for the meta field. $args can contain ‘before’ and ‘after’ arguments to wrap the description.

Loop for the field values

This is the full loop with the meta box and the loop going through the values for each field in the loop.

$metabox = gdcet_metabox($meta_box_name);

while ($metabox->have_fields()) :
  $metabox->the_field();

  if (gdcet_the_field()->have_values()) :
    while (gdcet_the_field()->have_values()) :
      gdcet_the_field()->the_value();

      // you can now work with the field and the value assigned to it //
     endwhile;
  endif;
endwhile;

What is important to see here is the loop for the field values. It starts with line 6 where we check if the field has value. Next line opens the WHILE loop, and line 8 setups the value for this field. If the field has more than one value, this loop will set them one by one.

Next articles will show how to work with the actual values including displaying them. Also, we will see how to work with custom meta fields compared to simple meta fields.

0
0
326
Rate this article

You are not allowed to rate this post.

Comments

Leave a Reply