Skip to content

Instantly share code, notes, and snippets.

@xymox12
Last active December 13, 2016 23:27
Show Gist options
  • Save xymox12/4718304 to your computer and use it in GitHub Desktop.
Save xymox12/4718304 to your computer and use it in GitHub Desktop.
Events manager - custom taxonomy and search filter
<?php
// custom host taxonomy for events
add_action( 'init', 'register_my_taxonomies', 0 );
function register_my_taxonomies() {
$labels = array(
'name' => 'Hosts',
'singular_name' => 'Host',
'search_items' => 'Search hosts',
'popular_items' => 'Popular hosts',
'all_items' => 'All hosts',
'parent_item' => 'Parent host',
'edit_item' => 'Edit host',
'update_item' => 'Update host',
'add_new_item' => 'Add New host',
'new_item_name' => 'New host',
'separate_items_with_commas' => 'Separate hosts with commas',
'add_or_remove_items' => 'Add or remove hosts',
'choose_from_most_used' => 'Choose from most used hosts'
);
$args = array(
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_nav_menus' => true,
'args' => array( 'orderby' => 'term_order' ),
'rewrite' => array( 'slug' => 'events/hosts', 'with_front' => false ),
'query_var' => true
);
register_taxonomy( 'hosts', EM_POST_TYPE_EVENT, $args );
}
// custom taxonomy search and display
add_action('em_template_events_search_form_ddm', 'hosts_search_form');
function hosts_search_form(){
$hosts = (is_array(get_option('hosts'))) ? get_option('hosts'):array();
?>
<!-- START hosts Search -->
<select name="host" id="host_search">
<option value="" selected="selected">Hosted by</option>
<?php
$taxonomies = array('hosts');
$args = array('orderby'=>'count','hide_empty'=>false);
echo get_terms_dropdown($taxonomies, $args);
?>
</select>
<!-- END hosts Search -->
<?php
}
function my_em_hosts_event_load($EM_Event){
global $wpdb;
$EM_Event->hosts = $wpdb->get_col("SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id='{$EM_Event->post_id}'", 0);
print_r($EM_Event->hosts);
}
add_action('em_event','my_em_hosts_event_load',1,1);
// And make the search attributes for the shortcode
add_filter('em_events_get_default_search','my_em_hosts_get_default_search',1,2);
function my_em_hosts_get_default_search($searches, $array){
if( !empty($array['host']) ){
// print_r($array['host']);
$searches['host'] = $array['host'];
}
return $searches;
}
add_filter('em_events_get','my_em_hosts_events_get',1,2);
function my_em_hosts_events_get($events, $args){
if( !empty($args['host']) ){
foreach($events as $event_key => $EM_Event){
if( !in_array($args['host'],$EM_Event->hosts) ){
unset($events[$event_key]);
}
}
}
return $events;
}
function get_terms_dropdown($taxonomies, $args){
$myterms = get_terms($taxonomies, $args);
foreach($myterms as $term){
$root_url = get_bloginfo('url');
$term_taxonomy=$term->taxonomy;
$term_slug=$term->slug;
$term_name =$term->name;
$value = $term->term_id;
$output .="<option value='".$value."'>".$term_name."</option>";
}
return $output;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment