Wordpress category not counting media attachments Wordpress category not counting media attachments wordpress wordpress

Wordpress category not counting media attachments


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 attachmentsfunction 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 taxonomyfunction renaissance_add_categories_to_attachments() {    register_taxonomy_for_object_type( 'category', 'attachment' );}add_action( 'init' , 'renaissance_add_categories_to_attachments' );


I've tested the answer from Victoria S and it is working.

If someone would like to avoid direct manipulation of WordPress globals, the below solution is based in 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!)