Skip to main content
All CollectionsDeveloper Documentation
How to get custom field on single event page
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...

Hakan Turan avatar
Written by Hakan Turan
Updated over 2 weeks ago

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

1

2

3

$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

1

2

3

4

5

6

$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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

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))) :

esc_html(stripslashes($value))).'</span>';

}

}

Did this answer your question?