0

I have the following code already working in the admin for my media library:

function atelier_add_iCat_taxonomy() {
    $labels = array(
        'name'              => 'iCats',
        'singular_name'     => 'iCat',
        'search_items'      => 'Search iCats',
        'all_items'         => 'All iCats',
        'parent_item'       => 'Parent iCat',
        'parent_item_colon' => 'Parent iCat:',
        'edit_item'         => 'Edit iCat',
        'update_item'       => 'Update iCat',
        'add_new_item'      => 'Add New iCat',
        'new_item_name'     => 'New iCat Name',
        'menu_name'         => 'iCat',
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'query_var' => 'true',
        'rewrite' => 'true',
        'show_admin_column' => 'true',
    );

    register_taxonomy( 'iCat', 'attachment', $args );
}
add_action( 'init', 'atelier_add_iCat_taxonomy' );

I would like a filter to make sure that images belonging to a certain category are only displayed.

Have tried the following, but it displays the main categories and not the custom taxonomy I have.

function atelier_add_image_category_filter() {
    $screen = get_current_screen();
    if ( 'upload' == $screen->id ) {
        $dropdown_options = array( 'show_option_all' => __( 'View all categories', 'iCats' ), 'hide_empty' => false, 'hierarchical' => true, 'orderby' => 'name', );
        wp_dropdown_categories( $dropdown_options );
    }
}
add_action( 'restrict_manage_posts', 'atelier_add_image_category_filter' );

Am I missing something or is there another solution?

  • Just a little note, The taxonomy name should only contain lowercase letters. I don't think that's the solution here, but it might save you some headaches too. – Dave Romsey Sep 26 '16 at 16:03
  • 1
    Thanks for the heads up @DaveRomsey - will amend site code – JCJ Sep 26 '16 at 16:15
1

You can add the taxonomy parameter.

function add_image_category_filter() {
    $screen = get_current_screen();
    if ( 'upload' == $screen->id ) {
        $dropdown_options = array( 
            'taxonomy' => 'YOUR_TAXONOMY', 
            'show_option_all' => __( 'View all categories', 'iCats' ), 
            'hide_empty' => true, 
            'hierarchical' => true,
             // default is cat which wouldn't filter custom taxonomies
            'value_field'       => 'slug',
            'name'              => 'YOUR_TAXONOMY', 
            'orderby' => 'name', );
        wp_dropdown_categories( $dropdown_options );
    }
}
add_action( 'restrict_manage_posts', 'add_image_category_filter' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.