Created
December 9, 2011 12:29
-
-
Save Ciantic/1451357 to your computer and use it in GitHub Desktop.
Filter in admin list
This file contains 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
<?PHP | |
abstract class My_AdminList { | |
public static $taxonomies = array("menu_category"); | |
public static $post_types = array("owns_restaurant_item"); | |
/** | |
* Admin init hook | |
*/ | |
public static function init() { | |
global $typenow; | |
if (!in_array($typenow, self::$post_types)) | |
return; | |
add_action("restrict_manage_posts", array(__CLASS__, 'filter_show_select')); | |
add_action("parse_query", array(__CLASS__, 'parse_query')); | |
} | |
/** | |
* Filter the query | |
* | |
* @param WP_Query $query | |
*/ | |
public static function parse_query(&$query) { | |
$tax_query = array(); | |
foreach (self::$taxonomies as $tax_slug) { | |
if (isset($_GET[$tax_slug])) { | |
$tax_query[] = array( | |
"taxonomy" => $tax_slug, | |
"field" => "slug", | |
"terms" => $_GET[$tax_slug] | |
); | |
} | |
} | |
if (!empty($tax_query)) { | |
$query->query_vars["tax_query"] = array_merge(array("relation" => "AND"), $tax_query); | |
} | |
} | |
/** | |
* Select box to filter (echo) | |
*/ | |
public static function filter_show_select() { | |
foreach (self::$taxonomies as $tax_slug) { | |
$tax_obj = get_taxonomy($tax_slug); | |
$tax_name = $tax_obj->labels->name; | |
$terms = get_terms($tax_slug); | |
// output html for taxonomy dropdown filter | |
echo "<select name='$tax_slug' id='$tax_slug' class='postform'>"; | |
echo "<option value=''>Show All $tax_name</option>"; | |
foreach ($terms as $term) { | |
echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>'; | |
} | |
echo "</select>"; | |
} | |
} | |
} | |
if (is_admin()) | |
add_action('load-edit.php', array('My_AdminList', 'init')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment