Skip to content

Instantly share code, notes, and snippets.

@yeriepiscesa
Last active April 20, 2019 04:06
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 yeriepiscesa/fe349efa90cca1a0b28ef1f185f021fa to your computer and use it in GitHub Desktop.
Save yeriepiscesa/fe349efa90cca1a0b28ef1f185f021fa to your computer and use it in GitHub Desktop.
<?php
function filter_events_by_taxonomies( $post_type, $which ) {
if ( 'event' !== $post_type )
return;
$taxonomies = array( 'event_category', 'event_location' );
foreach ( $taxonomies as $taxonomy_slug ) {
$taxonomy_obj = get_taxonomy( $taxonomy_slug );
$taxonomy_name = $taxonomy_obj->labels->name;
$terms = get_terms( $taxonomy_slug );
echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
echo '<option value="">' . sprintf( esc_html__( 'Show All %s', 'text_domain' ), $taxonomy_name ) . '</option>';
foreach ( $terms as $term ) {
printf(
'<option value="%1$s" %2$s>%3$s (%4$s)&lt;/option>',
$term->slug,
( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
$term->name,
$term->count
);
}
echo '</select>';
}
}
add_action( 'restrict_manage_posts', 'filter_events_by_taxonomies' , 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment