{"id":7432,"date":"2022-07-31T08:15:58","date_gmt":"2022-07-31T08:15:58","guid":{"rendered":"https:\/\/webnus.site\/dox\/?p=7432"},"modified":"2024-02-07T07:40:55","modified_gmt":"2024-02-07T07:40:55","slug":"how-to-get-custom-field-on-single-event-page","status":"publish","type":"knowledgebase","link":"https:\/\/webnus.net\/dox\/modern-events-calendar\/how-to-get-custom-field-on-single-event-page\/","title":{"rendered":"How to get custom field on single event page"},"content":{"rendered":"\n<p>In this article, you will learn How to get custom field on<a href=\"https:\/\/webnus.net\/modern-events-calendar\/events\/how-to-turn-negotiations-into-success\/\" target=\"_blank\" rel=\"noreferrer noopener\"> single event page<\/a>. The event custom fields data is stored in <a href=\"https:\/\/webnus.net\/dox\/modern-events-calendar\/what-are-the-best-php-settings-for-wordpress\/\" target=\"_blank\" data-type=\"knowledgebase\" data-id=\"1971\" rel=\"noreferrer noopener\">WordPress<\/a> meta, and you can access them using the get_post_meta function. Please check the following code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Get_custom_field_on_single_event_page\"><\/span>Get custom field on single event page<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$event_id = get_the_ID(); \/\/ Or you can get it from request parameters\n$data = get_post_meta($event_id, 'mec_fields', true);\nprint_r($data); \/\/ Temporarily print the data to see it\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Fields_configuration\"><\/span>Fields configuration<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You also need the Fields configuration<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n$main = MEC::getInstance(&#039;app.libraries.main&#039;);\n\n$fields = $main-&gt;get_event_fields();\nif(!is_array($fields) ) $fields = array();\n\nprint_r($fields); \/\/ Temporarily print the configuration to see the structure\n<\/pre><\/div>\n\n\n<p>Having both $fields and $data, you can print all the fields (or your desired fields) in the output. Check the following code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\nforeach($fields as $field_id =&gt; $item) {\n   \/\/ Not a field\n   if(!is_numeric($field_id)) continue;\n\n   \/\/ Only display desired fields\n   if(!in_array($field_id, &#x5B;1,2,3])) continue;\n\n   \/\/ Field Data\n   $result = isset($data&#x5B;$field_id]) ? $data&#x5B;$field_id] : NULL;\n\n   \/\/ Empty Value\n   if((!is_array($result) and trim($result) == &#039;&#039;) or (is_array($result) and !count($result))) continue;\n\n   $type = isset($item&#x5B;&#039;type&#039;]) ? $item&#x5B;&#039;type&#039;] : &#039;text&#039;;\n\n   if(isset($field&#x5B;&#039;label&#039;])) {\n      echo &#039;&lt;span class=&quot;mec-event-data-field-name&quot;&gt;&#039;.esc_html__(stripslashes($field&#x5B;&#039;label&#039;]), &#039;mec&#039;).&#039;: &lt;\/span&gt;&#039;;\n   }\n\n   if($type === &#039;email&#039;) {\n      echo &#039;&lt;span class=&quot;mec-event-data-field-value&quot;&gt;&lt;a href=&quot;mailto:&#039;.esc_attr($value).&#039;&quot;&gt;&#039;.esc_html($value).&#039;&lt;\/a&gt;&lt;\/span&gt;&#039;;\n   } elseif($type === &#039;tel&#039;) {\n      echo &#039;&lt;span class=&quot;mec-event-data-field-value&quot;&gt;&lt;a href=&quot;tel:&#039;.esc_attr($value).&#039;&quot;&gt;&#039;.esc_html($value).&#039;&lt;\/a&gt;&lt;\/span&gt;&#039;;\n   } elseif($type === &#039;url&#039;) {\n      echo &#039;&lt;span class=&quot;mec-event-data-field-value&quot;&gt;&lt;a href=&quot;&#039;.esc_url($value).&#039;&quot; target=&quot;_blank&quot;&gt;&#039;.esc_html($value).&#039;&lt;\/a&gt;&lt;\/span&gt;&#039;;\n   } elseif($type === &#039;date&#039;) {\n      $value = $main-&gt;to_standard_date($value);\n      echo &#039;&lt;span class=&quot;mec-event-data-field-value&quot;&gt;&#039;.esc_html($this-&gt;main-&gt;date_i18n($date_format, strtotime($value))).&#039;&lt;\/span&gt;&#039;;\n   } elseif($type === &#039;textarea&#039;) {\n      echo &#039;&lt;span class=&quot;mec-event-data-field-value&quot;&gt;&#039;.wpautop(stripslashes($value)).&#039;&lt;\/span&gt;&#039;;\n   } else {\n      echo &#039;&lt;span class=&quot;mec-event-data-field-value&quot;&gt;&#039;.(is_array($value) ? esc_html(stripslashes(implode(&#039;, &#039;, $value))) :\n   esc_html(stripslashes($value))).&#039;&lt;\/span&gt;&#039;;\n   }\n}\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>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 Fields configuration You also need the Fields configuration Having [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","template":"","knowledgebase_cat":[50],"class_list":["post-7432","knowledgebase","type-knowledgebase","status-publish","hentry","knowledgebase_cat-developer-other"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/webnus.net\/dox\/modern-events-calendar\/wp-json\/wp\/v2\/knowledgebase\/7432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webnus.net\/dox\/modern-events-calendar\/wp-json\/wp\/v2\/knowledgebase"}],"about":[{"href":"https:\/\/webnus.net\/dox\/modern-events-calendar\/wp-json\/wp\/v2\/types\/knowledgebase"}],"author":[{"embeddable":true,"href":"https:\/\/webnus.net\/dox\/modern-events-calendar\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/webnus.net\/dox\/modern-events-calendar\/wp-json\/wp\/v2\/comments?post=7432"}],"version-history":[{"count":3,"href":"https:\/\/webnus.net\/dox\/modern-events-calendar\/wp-json\/wp\/v2\/knowledgebase\/7432\/revisions"}],"predecessor-version":[{"id":10141,"href":"https:\/\/webnus.net\/dox\/modern-events-calendar\/wp-json\/wp\/v2\/knowledgebase\/7432\/revisions\/10141"}],"wp:attachment":[{"href":"https:\/\/webnus.net\/dox\/modern-events-calendar\/wp-json\/wp\/v2\/media?parent=7432"}],"wp:term":[{"taxonomy":"knowledgebase_cat","embeddable":true,"href":"https:\/\/webnus.net\/dox\/modern-events-calendar\/wp-json\/wp\/v2\/knowledgebase_cat?post=7432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}