Skip to content

Instantly share code, notes, and snippets.

@KhanMaytok
Created July 11, 2015 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KhanMaytok/f985090ca299a2fe15bc to your computer and use it in GitHub Desktop.
Save KhanMaytok/f985090ca299a2fe15bc to your computer and use it in GitHub Desktop.
Searchable new fields events manager
<?php
...
// START EM Custm Attribute "Teacher" search
/* Create a new search attribute form entry for our new Event Attribute "teacher" (#_ATT{teacher})
The values are hard-coded but could probably be automatically selected
Add this field to the search form using an EM action.
nb. The action doc'd here http://wp-events-plugin.com/tutorials/adding-custom-event-search-form-fields/
does not seem to exist so use the footer one for inclusion in advanced search or the header one
for normal search. Read the source Luke.
*/
function my_teacher_search_field(){ ?>
<div class="em-search-teacher em-search-field">
<label>Teacher</label>
<select name="teacher">
<option value=''>All Teachers</option>
<option value="Abi" <?php echo ((array_key_exists('teacher', $_POST)) && $_POST['teacher'] == "Abi") ? 'selected="selected"':''; ?>>Abi</option>
<option value="Lou" <?php echo ((array_key_exists('teacher', $_POST)) && $_POST['teacher'] == "Lou") ? 'selected="selected"':''; ?>>Lou</option>
</select>
</div>
<?php }
add_action('em_template_events_search_form_footer', 'my_teacher_search_field');
//
/* Hook the new search attribute teacher into default search filter,
so it isn’t ignored when Events Manager creates an events search query
*/
function my_teacher_search($searches, $array){
if( !empty($array['teacher']) ){
$searches['teacher'] = $array['teacher'];
}
return $searches;
}
add_filter('em_events_get_default_search','my_teacher_search',1,2);
/* This isn't needed. It seems that inclusion in the default_search filter
is all thats needed. See http://wp-events-plugin.com/tutorials/adding-custom-event-search-form-fields
*/
//function my_teacher_accepted_searches($searches){
// $searches[] = 'teacher';
// return $searches;
//}
//add_filter('em_accepted_searches','my_teacher_accepted_searches',1,1);
/* Can't hook into em_events_get filter as doc'd here
http://wp-events-plugin.com/tutorials/creating-custom-event-search-attributes/
as we need to join with kal_postmeta and not just filter kal_em_events
so append a subquery via sql_conditions hook to find kal_postmeta using our new search attribute and return valid
posts ids for main event query that is internally appended to display.
*/
function my_teacher_conditions($conditions, $args){
if( !empty($args['teacher'])) {
$teacherval = $args['teacher'];
$conditions['teacher'] = " (kal_em_events.post_id IN ( SELECT post_id FROM kal_postmeta WHERE (kal_postmeta.meta_key = 'teacher' AND kal_postmeta.meta_value = '$teacherval')))";
}
return $conditions;
}
add_filter( 'em_events_build_sql_conditions', 'my_teacher_conditions',1,2);
//
// END EM Custom Attribute Teacher Search
...
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment