Create Custom Fields for Event Posts
Learn to work with dates in WordPress.
In this lesson, we will create a new post type for events taking place in the school. The event posts will be sorted according to the date the event takes place.
Create custom post type—event
Creating a custom post type has been covered in the Registering Custom Post Type lesson in detail. Navigate to the mu-plugins folder inside the wp-content folder. This folder has a school-custom-post-types.php
file where we registered the teacher and course post types. Following the same steps, we will register the event post type.
The school-custom-post-types.php
file has a function school_custom_post_types
which hooks onto the init
hook during the WordPress initialization. To create a custom post type, the first step is to define the attributes. Create an array of arguments called $eventArgs
as follows:
<?php$eventArgs = array('public' => true,'show_in_rest' => true,'labels' => array('name' => 'Events','singular_name' => 'Event','add_new_item' => 'Add New Event','edit_item' => 'Edit Event','view_item' => 'View Event','all_items' => 'All Events'),'menu_icon' => 'dashicons-calendar-alt','rewrite'=> array('slug'=> 'events'),'has_archive' => true,'supports' => array( 'title', 'editor', 'excerpt' ));
The event post type supports an archive.
The default /event slug has been rewritten as /events.
We have used the calendar dashicon for the post type.
For an explanation of the rest of the attributes, visit the Custom post type arguments section.
Next, we will pass the array created above to the register_post_type()
function.
<?phpfunction school_custom_post_types() {//$teacherArgs array//$courseArgs array//$eventArgs arrayregister_post_type('teacher', $teacherArgs);register_post_type('course', $courseArgs);register_post_type('event', $eventArgs);}add_action('init', 'school_custom_post_types');
After saving changes, we can see the "Events" option in the admin dashboard sidebar.
Update permalink structure
WordPress does not update its permalink structure very often so it might not know that /events exists. To force an update of the permalink structure, go to "Settings" in the admin dashboard sidebar, then choose "Permalinks."
Since there is no need to change the way permalinks are being displayed on this screen, we will only click the "Save Changes" button. WordPress will regenerate the URLs thereby registering the /events slug.
Create a custom field group for event
For the event post type, there are some details that should be saved with every post. Every event takes place on a certain date, at a particular location and at a certain time. We will create a field group with fields for date, location and time. We will make this field group mandatory so that every new event must be created with a date, location, and time information.
Field names and types
Click the "ACF" option from the admin dashboard sidebar. Click "Field Groups" to create a field group and name it Event Details.
Create a field for the event date.
Provide Event Date as "Field Label". This automatically generates the "Field Name" as event_date. This name can be ...