I am creating a gallery that will automatically display new images from a chosen category and its subcategories. I have set up the categories so that they will apply to media using: register_taxonomy_for_object_type( 'category', 'attachment' );

Now I need to make it so that categories will count the related attachments not just posts.
I found this link How to Override default update_count_callback for category with this code:

function change_category_arg() {
    global $wp_taxonomies;
    if ( ! taxonomy_exists('category') )
        return false;

    $new_arg = &$wp_taxonomies['category']->update_count_callback;
    $new_arg->update_count_callback = 'your_new_arg';

add_action( 'init', 'change_category_arg' );

But as of yet I have not figured it out (not sure if it doesn't work or if I'm just not understanding something, such as what would be 'your_new_arg').
I did find the update_count_callback function option when registering a new taxonomy but I don't want to make my own, I want to use it with the existing category taxonomy.

Any help with this is much appreciated. Thanks!

1 Answer 1


You've probably solved this, but for anyone else that needs this:

$wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';

This is significant in the case of attachments. Because an attachment is a type of post, the default _update_post_term_count() will be used. However, this may be undesirable, because this will only count attachments that are actually attached to another post (like when you insert an image into a post). This means that attachments that you simply upload to WordPress using the Media Library, but do not actually attach to another post will not be counted. If your intention behind associating a taxonomy with attachments was to leverage the Media Library as a sort of Document Management solution, you are probably more interested in the counts of unattached Media items, than in those attached to posts. In this case, you should force the use of _update_generic_term_count() by setting '_update_generic_term_count' as the value for update_count_callback.

from Wordpress Codex on register_taxonomy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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