Problem: I would like to filter the list of Products by a custom taxonomy in wp-admin.
Solution: Add the following custom code to functions.php:
function filter_admin_lists_by_taxonomies( $post_type, $which ) { // Array of post type slugs where these filters should appear $filter_types = ['product']; // Array of taxonomy slugs to filter by $taxonomies = ['your-taxonomy-slug']; if ( !in_array( $post_type, $filter_types ) ) return; foreach ( $taxonomies as $taxonomy_slug ) { // Retrieve taxonomy data $taxonomy_obj = get_taxonomy( $taxonomy_slug ); $taxonomy_name = $taxonomy_obj->labels->name; // Retrieve taxonomy terms $terms = get_terms( $taxonomy_slug ); // Display filter HTML 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)</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_admin_lists_by_taxonomies' , 10, 2);
Modify the $filter_types and $taxonomies arrays as needed.
Relevant Documentation:
https://developer.wordpress.org/reference/hooks/restrict_manage_posts/
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | 8:00 – 12:00 | - | - |
13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | 13:00 – 17:00 | - | - |
Supporter timezone: America/New_York (GMT-04:00)
This topic contains 2 replies, has 2 voices.
Last updated by 6 years, 9 months ago.
Assisted by: Christian Cox.