Skip to content

Instantly share code, notes, and snippets.

@acodesmith
Last active March 28, 2018 18:54
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 acodesmith/f2ce253507362be360ad7b4335cd1714 to your computer and use it in GitHub Desktop.
Save acodesmith/f2ce253507362be360ad7b4335cd1714 to your computer and use it in GitHub Desktop.
Sorted taxonomy list sub menu links. Each taxonomy term generates a link in the admin menu for quick sorting based on terms.
<?php
add_action( 'admin_menu', 'add_taxonomy_sub_menus' );
function add_taxonomy_sub_menus()
{
$custom_post_type = 'shoes';
taxonomy_sub_menus( $custom_post_type );
}
function taxonomy_sub_menus($post_type)
{
global $submenu;
if( ! empty( $submenu[ "edit.php?post_type=$post_type" ] ) ) {
$program_menu = [];
$taxonomies = get_object_taxonomies( $post_type );
foreach( $submenu[ "edit.php?post_type=$post_type" ] as $key => $menu_item ) {
$program_menu[] = $menu_item;
foreach( $taxonomies as $taxonomy ) {
if( strpos( $menu_item[2], "taxonomy=$taxonomy" ) ) {
/** @var \WP_Term[] $terms */
$terms = get_terms( array(
'taxonomy' => $taxonomy,
'hide_empty' => false,
) );
if( ! empty( $terms ) )
foreach ( $terms as $term )
$program_menu[] = [
" - $term->name",
'edit_posts',
"edit.php?post_type=$post_type&$taxonomy=$term->slug",
$term->name,
];
}
}
}
$submenu[ "edit.php?post_type=$post_type" ] = $program_menu;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment