Archived topic
Custom Event Taxonomy Archive
5 replies · Started by Heidi on September 5, 2018
Hi again,
I think I'm out of my league, but am making a last-ditch effort to see if I'm anywhere near a solution that matches my skill level.
-
I created a custom taxonomy called "Event Types" for events created in The Events Calendar.
-
I use that "Event Types" taxonomy in a right sidebar drop-down menu so users can sort by Event Type from single event pages. Example > https://nusciencesolutions.com/event/healthy-meal-preparation-for-cancer-cold-soups/
-
The archive page that is returned for the sort comes from the standard archive.php. Example > https://nusciencesolutions.com/event_types/self-care/
This would be fine, except that my client has asked if we can sort by date, from earliest to latest. I'd also like to show the date of each event on the Event Type archive page. Since the Elementor archive widget template doesn't include Events Calendar dates in the meta data that can be displayed, I'd like the Event Type archive display to mirror that of the Events Calendar List format > https://nusciencesolutions.com/events/list/.
I assume this is possible with php. I tinkered a little, copying the GP archive.php, renaming it taxonomy-event_types.php, placing it in my generatepress_child folder, and playing around with pre_get_posts and other php code I found. Nothing worked.
So here I am to see if the brilliant experts at GP can/will help. Maybe there's an easier way that I'm missing? Totally cool if you tell me this is beyond your scope. Thanks!
Hi there,
Just so I understand, are you wanting your event_types archive to look like your events/list page?
I think you were on the right track with pre_get_posts, but it looks like the event_types is using Elementor, so I'm not sure if that's possible with them?
Hey Tom,
Yes to your first question (in a perfect world), and I am willing to forego the Elementor archive card skin layout to achieve it.
But if a list view isn't possible given how I've used Elementor to design every other aspect of the site, I will settle for simply being able to display sorted event_types, using the existing Elementor archive card skin layout, in ascending date order (soonest to latest).
If using pre_get_posts can give me the latter, I'll need to know exactly how to word/structure it and where to place it. Can I simply put it in Code Snippets? Or will I need to put it in a taxonomy-event_types.php file (copied from archives.php) and place in the GP child theme? If the latter, where do I place the pre_get_posts content? Before or in the Loop?
Thanks very much!
Hi there,
Give this a shot:
add_action( 'pre_get_posts', function( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_tax( 'event_types' ) ) {
$query->set( 'order', 'ASC' );
}
return $query;
} );
Let me know :)
Hi Tom,
Sorry for my delayed response. Thanks for pointing me in the right direction, as always! That code did switch the order, but I had to refine further to get exactly what I wanted. Here's what ultimately worked for me:
add_action( 'pre_get_posts', function( $query ) {
if ( ! is_admin() && $query->is_main_query() && is_tax( 'event_types' ) ) {
$query->set( 'meta_key', '_EventStartDate' );
$query->set( 'orderby', array( 'meta_value' => 'ASC' ));
}
return $query;
} );
Thanks again. Worth an extra donation for your ongoing stellar support!
Perfect! Thanks for sharing your code :)