Support Forum
I am trying to display one Custom Post Type 'Events' with custom fields on my home page. Post title and post content are working fine, but I am not able to display the custom fields, which are of type text, date and image.Kindly see the screenshot
https://drive.google.com/file/d/1Bu6cnQ6iDOzDPcBpB5ZnYNq_hKn-drwl/view?usp=sharing
Hi Vikas,
Please share the code that creates the custom field group, and sub-fields. I will check it on my end.
//Here is the Code For Events CPT
add_action( 'init', 'your_prefix_register_post_type' );
function your_prefix_register_post_type() {
$labels = [
'name' => esc_html__( 'Events', 'your-textdomain' ),
'singular_name' => esc_html__( 'Event', 'your-textdomain' ),
'add_new' => esc_html__( 'Add New Event', 'your-textdomain' ),
'add_new_item' => esc_html__( 'Add new event', 'your-textdomain' ),
'edit_item' => esc_html__( 'Edit Event', 'your-textdomain' ),
'new_item' => esc_html__( 'New Event', 'your-textdomain' ),
'view_item' => esc_html__( 'View Event', 'your-textdomain' ),
'view_items' => esc_html__( 'View Events', 'your-textdomain' ),
'search_items' => esc_html__( 'Search Events', 'your-textdomain' ),
'not_found' => esc_html__( 'No events found', 'your-textdomain' ),
'not_found_in_trash' => esc_html__( 'No events found in Trash', 'your-textdomain' ),
'parent_item_colon' => esc_html__( 'Parent Event:', 'your-textdomain' ),
'all_items' => esc_html__( 'All Events', 'your-textdomain' ),
'archives' => esc_html__( 'Event Archives', 'your-textdomain' ),
'attributes' => esc_html__( 'Event Attributes', 'your-textdomain' ),
'insert_into_item' => esc_html__( 'Insert into event', 'your-textdomain' ),
'uploaded_to_this_item' => esc_html__( 'Uploaded to this event', 'your-textdomain' ),
'featured_image' => esc_html__( 'Featured image', 'your-textdomain' ),
'set_featured_image' => esc_html__( 'Set featured image', 'your-textdomain' ),
'remove_featured_image' => esc_html__( 'Remove featured image', 'your-textdomain' ),
'use_featured_image' => esc_html__( 'Use as featured image', 'your-textdomain' ),
'menu_name' => esc_html__( 'Events', 'your-textdomain' ),
'filter_items_list' => esc_html__( 'Filter events list', 'your-textdomain' ),
'items_list_navigation' => esc_html__( 'Events list navigation', 'your-textdomain' ),
'items_list' => esc_html__( 'Events list', 'your-textdomain' ),
'item_published' => esc_html__( 'Event published', 'your-textdomain' ),
'item_published_privately' => esc_html__( 'Event published privately', 'your-textdomain' ),
'item_reverted_to_draft' => esc_html__( 'Event reverted to draft', 'your-textdomain' ),
'item_scheduled' => esc_html__( 'Event scheduled', 'your-textdomain' ),
'item_updated' => esc_html__( 'Event updated', 'your-textdomain' ),
'text_domain' => esc_html__( 'your-textdomain', 'your-textdomain' ),
];
$args = [
'label' => esc_html__( 'Events', 'your-textdomain' ),
'labels' => $labels,
'description' => '',
'public' => true,
'hierarchical' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'show_in_rest' => true,
'menu_position' => 2,
'query_var' => true,
'can_export' => true,
'delete_with_user' => true,
'has_archive' => true,
'rest_base' => '',
'show_in_menu' => true,
'menu_icon' => 'dashicons-admin-users',
'capability_type' => 'post',
'supports' => ['title', 'editor', 'thumbnail'],
'taxonomies' => [],
'rewrite' => [
'with_front' => false,
],
];
register_post_type( 'event', $args );
}
//Here is the Code for MetaBoxes attached to Events CPT
add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
function your_prefix_register_meta_boxes( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => esc_html__( 'Event Fields Group', 'text-domain' ),
'id' => 'event_fields_group',
'post_types' => ['event'],
'context' => 'normal',
'priority' => 'high',
'fields' => [
[
'id' => $prefix . 'event_date',
'type' => 'date',
'name' => esc_html__( 'Event Date', 'text-domain' ),
],
[
'id' => $prefix . 'event_speaker',
'type' => 'text',
'name' => esc_html__( 'Event Speaker', 'text-domain' ),
],
[
'id' => $prefix . 'event_speaker_image',
'type' => 'image',
'name' => esc_html__( 'Event Speaker Image', 'text-domain' ),
],
],
];
return $meta_boxes;
}
Hi,
To get the field value, you just need to pass the field ID to the first argument of the helper function.
mb.rwmb_meta( 'event_speaker', '', event.ID )
Get more details in the documentation https://docs.metabox.io/rwmb-meta/#arguments
Hi Long,
I tried but not able to display. I am somewhat familiar with PHP and but not with Twig.
So for the time being, I will use Advanced Custom Fields for my template. But I will surely give MB Views a try, when there will be a detailed documentation with a simple example on using and displaying Custom Post Types with MetaBoxes (custom fields)in the MB Views for example on Home Page.
Hi Vikas,
Maybe the cache still loads the first time. Please try to clear the cache and check the field value again. Here is the screen record I've tried to use your code on my local site https://share.getcloudapp.com/NQuK5pZq.
Thanks, that worked.
Kindly also advise, on how to display the image in twig, like in the above example, there is one custom field with ID event_speaker_image. How to display that?
Further, is it possible, to write simple PHP instead of twig in the views? I tried to write PHP code, but the output is the code itself and not the result.
I have found a cookbook regarding functionality of twig with customfields, but it is more focused on ACF, so not much of help.
https://timber.github.io/docs/guides/acf-cookbook/
Hi,
The setting multiple
of the field image
is set to true
by default so the helper function will return an array of images. You need to loop through the value return, example:
{% set images = mb.rwmb_meta( 'event_speaker_image', '', event.ID) %}
{% for image in images %}
<img src="{{image['full_url']}}" />
{% endfor %}
Get more details in the documentation https://docs.metabox.io/fields/image/#template-usage.
Regarding the PHP code with Twig, it would be better if you create your own function by PHP code then use it in the View via the proxy mb.
.
Okay Thanks.
Yes, PHP function with proxy mb. is a good idea. I will try it, and will contact, in case I requre further help.
Thanks once again.
It is a sort of case study request, if possible.
Kindly create a guide containing simple examples on various custom fields and how to display those in MB Views. It will be very helpful, to cut the time, which is wasted in searching various online sources to do simple things, but unfortunately, there are very minimal tutorials/resources, when it comes to Metabox, as compared to ACF, Toolset and Jetengine.
Hi,
Thanks for your feedback.
If you search for some cases to show the field value on this forum, such as https://support.metabox.io/topic/display-multiple-media-files-connected-to-a-user/
combine with the documentation of MB Views https://docs.metabox.io/extensions/mb-views/#insert-fields and field https://docs.metabox.io/fields/image/#template-usage. You will see that it's so easy to show the field value.
We will also try to improve the documentation to help new users understand more to use the View.