How to get custom field on single event page

In this article, you will learn How to get custom field on single event page. The event custom fields data is stored in WordPress meta, and you can access them using the get_post_meta function. Please check the following code.

Get custom field on single event page

$event_id = get_the_ID(); // Or you can get it from request parameters
$data = get_post_meta($event_id, 'mec_fields', true);
print_r($data); // Temporarily print the data to see it

Fields configuration

You also need the Fields configuration

$main = MEC::getInstance('app.libraries.main');

$fields = $main->get_event_fields();
if(!is_array($fields) ) $fields = array();

print_r($fields); // Temporarily print the configuration to see the structure

Having both $fields and $data, you can print all the fields (or your desired fields) in the output. Check the following code.

foreach($fields as $field_id => $item) {
   // Not a field
   if(!is_numeric($field_id)) continue;

   // Only display desired fields
   if(!in_array($field_id, [1,2,3])) continue;

   // Field Data
   $result = isset($data[$field_id]) ? $data[$field_id] : NULL;

   // Empty Value
   if((!is_array($result) and trim($result) == '') or (is_array($result) and !count($result))) continue;

   $type = isset($item['type']) ? $item['type'] : 'text';

   if(isset($field['label'])) {
      echo '<span class="mec-event-data-field-name">'.esc_html__(stripslashes($field['label']), 'mec').': </span>';

   if($type === 'email') {
      echo '<span class="mec-event-data-field-value"><a href="mailto:'.esc_attr($value).'">'.esc_html($value).'</a></span>';
   } elseif($type === 'tel') {
      echo '<span class="mec-event-data-field-value"><a href="tel:'.esc_attr($value).'">'.esc_html($value).'</a></span>';
   } elseif($type === 'url') {
      echo '<span class="mec-event-data-field-value"><a href="'.esc_url($value).'" target="_blank">'.esc_html($value).'</a></span>';
   } elseif($type === 'date') {
      $value = $main->to_standard_date($value);
      echo '<span class="mec-event-data-field-value">'.esc_html($this->main->date_i18n($date_format, strtotime($value))).'</span>';
   } elseif($type === 'textarea') {
      echo '<span class="mec-event-data-field-value">'.wpautop(stripslashes($value)).'</span>';
   } else {
      echo '<span class="mec-event-data-field-value">'.(is_array($value) ? esc_html(stripslashes(implode(', ', $value))) :

Report Out Dated Content

If you think the content of this page does not reflect updated information, please let us know.

Report Now

No Comment.