Group event search form results by all day, morning, afternoon, evening
-
What is the best approach to grouping search form results by timeframes within a day?
- All Day Events
- Morning Events
- Afternoon Events
- Evening Events
I’m happy to write custom queries but I don’t know which file to modify.
That’s assuming a template is the best approach.
Perhaps there’s another better way?
-
You could try creating a custom scope for those time periods so that they can be used with the [events_list] shortcode:
https://wp-events-plugin.com/tutorials/create-your-own-event-scope/
Thanks Calmin_nwl, That sounds like a good approach.
The site I’m building is heavily based around the AJAX search form (staging site here).
What’s the best place to put the shortcodes so that they will be used in the results container: .em-search-ajax?
I think the shortcode calls the events-list.php plugin template file, so they can’t be used in there.. just creates an infinite loop.
So maybe.. a filter function?
I’m not following 100% what you’re trying to do. Can you go into a bit more detail?
Each time someone does a search – filtered by date and category – they should get the following results in the AJAX container:
All Day Events
4 events, filtered by chosen criteriaMorning Events
4 events, filtered by chosen criteriaAfternoon Events
4 events, filtered by chosen criteriaEvening Events
4 events, filtered by chosen criteriaCurrently I have the filtering working fine, (as per my staging site). I just need to split the results into the 4 time periods now. Your suggestion of creating a custom scope (or rather 4 custom scopes) sounds like what I was after.
I think perhaps using the custom scopes in template tags or the object::command structure (my own terminology.. hope that’s correct) may be better than using the scopes in a shortcode for reasons outlined below, as far as I understand things.
If I understand correctly, this is what the plugin is doing:
————————
[event_search_form ajax=”1″]
Shortcode on WordPress page.
▼
events-search.php ?? modified /templates/search/scope.php
Template file called by event_search shortcode.
$args from plugin settings.
▼
<div class=”em-search-ajax“></div>
Results container populated by search form results.
▼
events-list.php
Template file used in search results.
▼
Plugin settings: event list formatting————————
So it’s the events-list.php which needs to be modified to output four scope-based loops instead of one. Or alternatively a filter function can be used.
————————
So working through the available options, all using custom scopes except for the last one:
1. Shortcodes in WordPress page template
Not really an option because AJAX is being used.2. Shortcodes in events-list.php template file
Could have been an option, but it doesn’t work. I can create custom scopes, one for each time period, and put them into [events_list] shortcodes. But if I use the shortcodes in the events-list.php template file I get a white screen. I think the shortcode fetches the template file, so you end up with an infinite loop.3. Shortcodes in filter function
So as far as I understand the only remaining option that can use a shortcode – albeit via do_shortcode() – is a filter in functions.php, probably em_event_output.4. Template tag or object::command (with args) in filter function
But if I’m using the filter then I may as well use template tags like em_events( $args), or an object::command like EM_Events::output( $args ) instead of shortcodes.5. EM_Events::output($args) in events-list.php template file
Lastly, if we’re using template tags or object::command instead of shortcodes, there’s another option available: using those in the events-list.php template file.6. Four time-based wp_query loops in filter function
Just listing this because it’s a possible method. Only use WordPress (non EM) functions to build the loops. Custom scopes not needed in this case.————————
Option 1 doesn’t work
Option 2 doesn’t work
Option 3 seems slightly excessive
Option 4 would probably be most versatile and robust
Option 5 seems simplest
Option 6 not necessaryHopefully I’ve explained my requirements well.
Are these all the available options, or are there others which are potentially better?
Is my assessment of the suitability of option 4 and 5 correct?Thanks for the help, Calmin_nwl
Seeing as the scope attribute was already being used to query the date, I created a custom search attribute instead.
It works, although seems a little slow to query, and I still need to add All Day events.
In functions.php
/* Hook timeperiod search attribute into default search filter */ function timeperiod_search($searches, $array){ if( !empty($array['timeperiod']) ){ $searches['timeperiod'] = $array['timeperiod']; } return $searches; } add_filter('em_events_get_default_search','timeperiod_search',1,2); /* Filter Conditions */ function timeperiod_conditions($conditions, $args){ if( !empty($args['timeperiod'])) { $timeperiodval = $args['timeperiod']; if ($timeperiodval == "Morning") { $period_start_time = "06:00:00"; $period_end_time = "12:00:00"; } elseif ($timeperiodval == "Afternoon") { $period_start_time = "12:00:00"; $period_end_time = "18:00:00"; } elseif ($timeperiodval == "Evening") { $period_start_time = "18:00:00"; $period_end_time = "06:00:00"; } $conditions['timeperiod'] = " (event_start_time BETWEEN CAST('$period_start_time' AS TIME) AND CAST('$period_end_time' AS TIME))"; } return $conditions; } add_filter( 'em_events_build_sql_conditions', 'timeperiod_conditions',1,2);
In events-list.php template file:
<?php $args = apply_filters('em_content_events_args', $args); if( get_option('dbem_css_evlist') ) echo "<div class='css-events-list'>"; $args['timeperiod'] = 'Morning'; echo "<h2>Morning</h2>"; echo EM_Events::output( $args ); $args['timeperiod'] = 'Afternoon'; echo "<h2>Afternoon</h2>"; echo EM_Events::output( $args ); $args['timeperiod'] = 'Evening'; echo "<h2>Evening</h2>"; echo EM_Events::output( $args ); if( get_option('dbem_css_evlist') ) echo "</div>";
Ok, I guess it’s slightly slow because it’s running multiple queries.
Any way around that? With this, or another method?sorry but at the moment we don’t have any workaround for this at the moment.
- The topic ‘Group event search form results by all day, morning, afternoon, evening’ is closed to new replies.