Articles on: Developer

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 ''.esc_html__(stripslashes($field['label']), 'mec').': ';
}


if($type === 'email') {
echo ''.esc_html($value).'';
} elseif($type === 'tel') {
echo ''.esc_html($value).'';
} elseif($type === 'url') {
echo ''.esc_html($value).'';
} elseif($type === 'date') {
$value = $main->to_standard_date($value);
echo ''.esc_html($this->main->date_i18n($date_format, strtotime($value))).'';
} elseif($type === 'textarea') {
echo ''.wpautop(stripslashes($value)).'';
} else {
echo ''.(is_array($value) ? esc_html(stripslashes(implode(', ', $value))) :
esc_html(stripslashes($value))).'';
}
}

Updated on: 01/02/2026

Was this article helpful?

Share your feedback

Cancel

Thank you!