Advertisement
Guest User

Untitled

a guest
Dec 8th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. <?php
  2. if (!class_exists('Tax_CTP_Filter')){
  3. /**
  4. * Tax CTP Filter Class
  5. * Simple class to add custom taxonomy dropdown to a custom post type admin edit list
  6. * @author Ohad Raz <admin@bainternet.info>
  7. * @version 0.1
  8. */
  9. class Tax_CTP_Filter
  10. {
  11. /**
  12. * __construct
  13. * @author Ohad Raz <admin@bainternet.info>
  14. * @since 0.1
  15. * @param array $cpt [description]
  16. */
  17. function __construct($cpt = array()){
  18. $this->cpt = $cpt;
  19. // Adding a Taxonomy Filter to Admin List for a Custom Post Type
  20. add_action( 'restrict_manage_posts', array($this,'my_restrict_manage_posts' ));
  21. }
  22.  
  23. /**
  24. * my_restrict_manage_posts add the slelect dropdown per taxonomy
  25. * @author Ohad Raz <admin@bainternet.info>
  26. * @since 0.1
  27. * @return void
  28. */
  29. public function my_restrict_manage_posts() {
  30. // only display these taxonomy filters on desired custom post_type listings
  31. global $typenow;
  32. $types = array_keys($this->cpt);
  33. if (in_array($typenow, $types)) {
  34. // create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
  35. $filters = $this->cpt[$typenow];
  36. foreach ($filters as $tax_slug) {
  37. // retrieve the taxonomy object
  38. $tax_obj = get_taxonomy($tax_slug);
  39. $tax_name = $tax_obj->labels->name;
  40.  
  41. // output html for taxonomy dropdown filter
  42. echo "<select name='".strtolower($tax_slug)."' id='".strtolower($tax_slug)."' class='postform'>";
  43. echo "<option value=''>Show All $tax_name</option>";
  44. $this->generate_taxonomy_options($tax_slug,0,0,(isset($_GET[strtolower($tax_slug)])? $_GET[strtolower($tax_slug)] : null));
  45. echo "</select>";
  46. }
  47. }
  48. }
  49.  
  50. /**
  51. * generate_taxonomy_options generate dropdown
  52. * @author Ohad Raz <admin@bainternet.info>
  53. * @since 0.1
  54. * @param string $tax_slug
  55. * @param string $parent
  56. * @param integer $level
  57. * @param string $selected
  58. * @return void
  59. */
  60. public function generate_taxonomy_options($tax_slug, $parent = '', $level = 0,$selected = null) {
  61. $args = array('show_empty' => 1);
  62. if(!is_null($parent)) {
  63. $args = array('parent' => $parent);
  64. }
  65. $terms = get_terms($tax_slug,$args);
  66. $tab='';
  67. for($i=0;$i<$level;$i++){
  68. $tab.='--';
  69. }
  70.  
  71. foreach ($terms as $term) {
  72. // output each select option line, check against the last $_GET to show the current option selected
  73. echo '<option value='. $term->slug, $selected == $term->slug ? ' selected="selected"' : '','>' .$tab. $term->name .' (' . $term->count .')</option>';
  74. $this->generate_taxonomy_options($tax_slug, $term->term_id, $level+1,$selected);
  75. }
  76.  
  77. }
  78. }//end class
  79. }//end if
  80. /*
  81. //usage:
  82. new Tax_CTP_Filter(array(
  83. 'CUSTOM_POST_TYPE_NAME' => array('CUSTOM_TAXONOMY_NAME2','CUSTOM_TAXONOMY_NAME2')
  84. )
  85. );
  86. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement