WordPress category not counting media attachments

In WordPress 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:

Read More
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!

Related posts

Leave a Reply

2 comments

  1. Hopefully this helps anyone that also had this problem. This is what I ended up putting in functions.php:

    //Update Category count callback to include attachments
    function change_category_arg() {
        global $wp_taxonomies;
        if ( ! taxonomy_exists('category') )
            return false;
    
        $wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';
    }
    add_action( 'init', 'change_category_arg' );
    
    //Add Categories taxonomy
    function renaissance_add_categories_to_attachments() {
        register_taxonomy_for_object_type( 'category', 'attachment' );
    }
    add_action( 'init' , 'renaissance_add_categories_to_attachments' );
    
  2. I’ve tested the answer from Victoria S and it is working.

    But, if someone would like to avoid direct manipulation of WordPress globals, the below solution is based on native WordPress functions.

    function my_add_categories_to_attachments() {
        $myTaxonomy = get_taxonomies(array('name' => 'category'), 'objects')['category'];
        $myTaxonomy->update_count_callback = '_update_generic_term_count';
        register_taxonomy ('category',
                           $myTaxonomy->object_type,
                           array_merge ((array) $myTaxonomy,
                                        array('capabilities' => (array) $myTaxonomy->cap)));
    
        register_taxonomy_for_object_type( 'category', 'attachment' );
    }
    add_action( 'init' , 'my_add_categories_to_attachments' );
    

    The key here is that register_taxonomy is used to recreate the category taxonomy identically, but changing the update_count_callback function. We use the taxonomy object from get_taxonomies assigned to $myTaxonomy.

    • The first argument is the Taxonomy slug we’d like to change: 'category'
    • The second argument is the array of object (post) types. We use it from the object returned by the get_taxonomies.
    • The third argument ($args) is an array of the properties of the Taxonomy. We have to make sure to include everything properly from $myTaxonomy, to make sure the recreated category is identical to the original one, except for the changes we want, in this case modify the update_count_callback to use _update_generic_term_count instead of the default _update_post_term_count. The only issue is with the capabilities property, as it must be passed as capabilities, but is stored in the Taxonomy object as cap, therefore we need to extend the array of $args with the cap object cast as an array under the label of capabilities.

    Please note, that for some reasons during my tests I saw the labels array of the recreated Taxonomy to include one additional item (["archives"]=>"All Categories") compared to the original. This should not affect the system, as an additional label not referenced anywhere should not cause issues.

    You can easily compare the Taxonomy before and after editing to make sure everything is in order by using var_dump(get_taxonomies(array('name' => 'category'), 'objects')['category']). (Do not do that on production site unless you know what you are doing!)