Showing upcoming events - Meta Box + Bricks
Sharing what’s coming up helps us see important dates and deadlines, so we can manage events better. Take the upcoming events as an example. Let's use Meta Box and Bricks to make it happen!
Video version
Preparation
This is the rule: just events with the end date after the current day could be displayed. Day by day, if it passes the end date of any event, those events will automatically disappear from the page.
Assume that today is December 8th.
We will filter and display the events that are either ongoing or scheduled for the future. This means that an event with an end date beyond December 8th will be shown on the page.
For some basic event details, I’ll create some custom fields. The start date and the end date as well as will be used for that filter. And in this case, each event will be a post of a custom post type.
Here's what we'll use:
- Meta Box plugin: to have a framework to create a custom post type and custom fields for the events. You can install it directly from wordpress.org;
- MB Custom Post Type: to create a custom post type for the events;
- Meta Box Builder: to have a UI on the backend to create the custom fields visually;
And, I use Bricks to build the page.
Now, let’s start!
1. Creating a new post type
Go to Meta Box > Post Types to create a new post type for the events.
After publishing, you’ll see a new menu. It’s your post type.
2. Creating custom fields
Each event may have some subsidiary information. Then, we should use custom fields to store them. I have some typical ones for this practice.
Two of them, the start date and end date should be must-have items since they will be used for the filter.
Now, go to Meta Box > Custom Fields to create them.
Choose the Date Picker field type for the start and end date of the event.
To display the date on the management dashboard for easy comparison with the results, turn on the button like in the image below. This setting is available only when enabling the MB Admin Columns extension. This extension is just optional, so I did not mention it at the beginning.
After creating all the needed fields, go to the Settings tab > choose Location as Post type > select Event to apply these fields to this post type.
Now, in the post editor, you will see the created custom fields.
Just input data into them.
Here are some posts that I created for example:
The start date and the end date are shown as admin columns. You may want to see these ones once again in the end to easily compare with the ones displayed on the page.
3. Displaying all events on the page
Now, edit a page with Bricks.
First, select a Section element.
Next, add a Heading element, and name the title.
In the Container element inside the section, select the Div element to contain the event’s information.
Then, to get all posts in the Event post type, enable the Use query loop button. In the Query section, choose the Type as Posts. And, choose the post type you want to get data from in the Post type section.
Now, let’s add some elements to get detailed information of the events.
For the image of the event, choose the Image element. Since these images are the featured image of the post, click the Select dynamic data icon and choose the Featured Image option.
As you can see, the image is displayed in the preview.
Next, add the Post Title element for the event title.
Now, I want to display the start date, end date, and location along with icons, so choose the Icon Box element instead of normal text.
You can style, and choose any icon as you want.
As this information is saved in custom fields created with Meta Box, we’ll use the Select dynamic data, and then choose the corresponding fields.
As a result, you’ll see a date from the Start Date field displayed immediately.
Do the same with the end date and location. Just duplicate the Icon Box element for saving time, then change the corresponding information.
We’ve just finished getting all the posts in the event post type. Note that I specifically mentioned "all the posts".
To style this section, choose each element and change its settings.
Look at this picture, you can see all the events shown on the frontend with styling.
To display only the upcoming events, we need a custom query to filter those posts. Let’s move to the next step.
4. Quering to show the upcoming events
It’s great when Bricks supports creating any custom query on the screen. So in the page editor with Bricks, go back to the Query section of the div, look for the Meta Query section, and add a meta query.
We should compare the end date with the current date. If the events have the end date after the current date, they could be displayed. Let’s set the condition like this:
In there:
- In the Meta key box, enter the ID of the field named End Date.
- In the Meta Value box, add the current date with the
{current_date:Y-m-d}
format. - In the Compare box, choose the Greater than or equal option to get the events that have the end date after the current date.
That’s all for the condition!
On the frontend, just events have the end date after the current day, which I marked at the beginning could be displayed.
Should you need to display posts with additional specific criteria, you can refer to it. Hope it can be helpful to you!