I was trying to show a list of events.
A little agenda with max 3 items. The first 3 events coming up.
But WordPress will show all events (max 3 which I set up) and isn’t, by default, capable to filter the past items out of it.
But here is a solution to only show the future events.
I created a custom post type named events:
And with the post Type Key pt_events
I created 2 fields in it, a Event Name and the event date:
Make sure that the event date is in a format which is sortable like the US time format:
For displaying the dae in front-end you can set the format here:
After that I created 5 events. 1 in the past and 4 in te future:
I created a new page and added a query in it:
I opened the Query Editor and Edited the query a little bit:
I added a quert Template with a Div and 2 Paragraphs to show the event date and event name:
Select the first paragraph, click to edit Dynamic Content and select the ACF-field of the Event Date:
And do the same for the 2nd paragraph but show here the name:
You now see that all events are listed. Not only 3 AND the one in the past.
Let’s change that, in the query editor on the pagination part set the number of items to 3:
And change the order from Published Date DESC to Event Date ASC:
This all can be managed with Cwicly, ACF en CPT.
The only problem is that event in the past. So lets fix that too.
Add a Code Block before the query and add the code to it with the correct id and with the date of today. You can find the code below at the bottom of this page
(p.s. if you can’t add code to it, make sure that in the Cwicly Role Editior The Code Block PHP is toggled on)
Now we have to edit to query to look at this date by using the Meta Query:
To view the result you have to view the page in front-end view:
That’s it!
<?php
add_filter( ‘cwicly/query/args’, function( $query_args, $attributes, $id ) {
if ($id === ‘query-caccc97’) {
$query_args[‘meta_query’][0][‘value’] = date( ‘Y-m-d’ );
}
return $query_args;
}, 10, 3 );
?>
Design by Lijn-B
Made with Cwicly
Leave a Reply