×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Tommaso Vietina
Added: Mar 7, 2023 2:20 PM
Views: 0
Tags: no tags
  1. add_action( 'restrict_manage_posts', 'filter_backend_by_taxonomies', 99, 2 );
  2. function filter_backend_by_taxonomies( $post_type, $which ) {
  3.        
  4.         if ( 'gallery' !== $post_type ) {
  5.                 return;
  6.         }
  7.        
  8.         $taxonomies = array( 'gallery_location', 'gallery_materials' );
  9.        
  10.         foreach ( $taxonomies as $taxonomy_slug ) {
  11.                
  12.                 // Retrieve taxonomy data
  13.                 $taxonomy_obj  = get_taxonomy( $taxonomy_slug );
  14.                 $taxonomy_name = $taxonomy_obj->labels->name;
  15.                
  16.                 // Retrieve taxonomy terms
  17.                 $terms = get_terms( $taxonomy_slug );
  18.                
  19.                 // Display filter HTML
  20.                 echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
  21.                 echo '<option value="">' . sprintf( esc_html__( 'Show All %s', 'text_domain' ), $taxonomy_name ) . '</option>';
  22.                 foreach ( $terms as $term ) {
  23.                         printf(
  24.                                 '<option value="%1$s" %2$s>%3$s (%4$s)</option>',
  25.                                 $term->slug,
  26.                                 ( ( isset( $_GET[ $taxonomy_slug ] ) && ( $_GET[ $taxonomy_slug ] == $term->slug ) ) ? ' selected="selected"' : '' ),
  27.                                 $term->name,
  28.                                 $term->count
  29.                         );
  30.                 }
  31.                 echo '</select>';
  32.         }
  33. }