Last active
December 12, 2015 04:58
-
-
Save xymox12/4718315 to your computer and use it in GitHub Desktop.
Events Manager - Search/filter but custom attribute (field) v.03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('em_template_events_search_form_ddm', 'my_em_styles_search_form'); | |
function my_em_styles_search_form(){ | |
$custom_attrs = get_meta_values('Hosted_by','Event'); | |
?> | |
<!-- START Styles Search --> | |
<select name="host"> | |
<option value=''>All Styles</option> | |
<?php foreach($custom_attrs as $meta_name): ?> | |
<option value="<?php echo $meta_name; ?>" <?php echo ($_GET['host'] == $meta_name) ? 'selected="selected"':''; ?>><?php echo $meta_name; ?></option> | |
<?php endforeach; ?> | |
</select> | |
<!-- END Styles Search --> | |
<?php | |
} | |
function get_meta_values( $key = '', $type = 'post', $status = 'publish' ) { | |
global $wpdb; | |
if( empty( $key )){ | |
return; | |
} | |
$r = $wpdb->get_col( $wpdb->prepare( " | |
SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm | |
LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id | |
WHERE pm.meta_key = '%s' | |
AND p.post_status = '%s' | |
AND p.post_type = '%s' | |
", $key, $status, $type ) ); | |
return $r; | |
} | |
?> | |
<?php | |
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']) ){ | |
$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( $args['host'] != $EM_Event->event_attributes['Hosted_by']){ | |
unset($events[$event_key]); | |
} | |
} | |
} | |
return $events; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment