Skip to content
No results
MEC Knowledgebase
  • Installation
    • Installation
    • Activation
    • Auto Update
    • Manual Update
    • Change License Domain
  • Settings
    • General
    • Archive Page
    • Slugs/Permalinks
    • Currency
    • View All
  • Single Event
    • Single Event Settings
    • How to Add Event In MEC Plugin?
    • MEC Single Sidebar
    • Custom Fields
    • View All
  • Event Modules
    • Speakers
    • Organizers
    • Locations
    • Map
    • View All
  • Booking Settings
    • Booking Module
    • Booking Elements
    • Booking Form
    • Payment Gateways
    • View All
  • Event Submission
    • General
    • Access Level
    • FES Sections
    • Required Fields
    • View All
  • Integrations
    • Mailchimp
    • Paid Memberships Pro
    • BuddyPress
    • LearnDash
    • View All
  • Notifications
    • Booking Notification
    • Booking Confirmation
    • Booking Reminder
    • Admin Notification
    • View All
  • Addons
    • Elementor Single Builder Addon
    • Ticket and Invoice Addon
    • Event Seat Addon
    • Virtual Events Addon
    • View All
  • Other
    • Appearance Settings
    • Translation
    • Knowledge
    • Troubleshooting
    • Developer
MEC Knowledgebase

  • Categories
    • Installation
      • Installation
      • Activation
      • Auto Update
      • Manual Update
      • Change License Domain
    • General Settings
      • General
      • Archive Pages
      • Slugs/Permalinks
      • Currency
      • View All
    • Single Event
      • Single Event Settings
      • MEC Single Sidebar
      • How to Add Event In MEC Plugin?
      • Custom Fields
      • View All
    • Event Modules
      • Speakers
      • Organizers
      • Locations
      • Map
      • View All
    • Booking Settings
      • Booking Module
      • Add a Booking System
      • Booking Form
      • Organizer Payment Settings
      • View All
    • Event Submission
      • General
      • Access Level
      • FES Sections
      • Required Fields
      • View All
    • MEC Integrations
      • Mailchimp
      • BuddyPress
      • LearnDash
      • Paid Memberships Pro
      • View All
    • Notifications
      • Booking Notification
      • Booking Confirmation
      • Booking Reminder
      • Admin Notification
      • View All
    • Addons
      • Elementor Single Builder Addon
      • Ticket and Invoice Addon
      • Event Seat Addon
      • Virtual Events Addon
      • View All
    • Other
      • Appearance Settings
      • Translation
      • Knowledge
      • Troubleshooting
      • Developer

Submit a Ticket
MEC Knowledgebase

Toggle
  • Get custom field on single event page
  • Fields configuration

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 '<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>';
   }
}

Share

Report Out Dated Content

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

Report Now
Login Submit
0 Comments

No Comment.

Still no luck? We can help!

If you did not find what you were looking for, please submit a ticket in our support system.

Submit a Ticket
Webnus Banner
Modern Events Calendar

Please read our Terms and Conditions and Privacy Policy.

Review us on Trustpilot

Copyright © 2023. All rights reserved. Webnus Inc.