Support » Plugin: Media Library Assistant » Incorrect Att. Tags Count Saved to term_taxonomy Table

  • Resolved Orin


    I haven’t figured out the rule for how far off, but they’re always off by a consistent amount. I’ve manually adjusted the count to see if that helps but if I add more att. tags it reverts to a (new) bad count. So nothing is wrong with saving the number to the database, just the calculation of that number. I have a high number of attachments with tags (2000+) so I’m wondering if this is the issue.

    It only affects areas that directly call the ‘count’ value from term_taxonomy. On the Att. Tags page, for instance, the correct count is listed in the attachments column.

    If there’s any more diagnostic information I could give, I’d be happy to help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Huh. Actually, I think it has something to do with so many of them not being assigned to a parent post.

    Plugin Author David Lingren



    Thanks for your interest in the plugin and for this question. The incorrect counts is a problem I noticed early on and addressed in one of my FAQ entries:

    Why don’t the “Posts” counts in the taxonomy edit screens match the search results when you click on them?

    This is a known WordPress problem with multiple support tickets already in Trac, e.g., Ticket #20708(closed defect (bug): duplicate) Wrong posts count in taxonomy table, Ticket #14084(assigned defect (bug)) Custom taxonomy count includes draft & trashed posts, and Ticket #14076(closed defect (bug): duplicate) Misleading post count on taxonomy screen.

    For example, if you add Tags support to the Assistant and then assign tag values to your attachments, the “Posts” column in the “Tags” edit screen under the Posts admin section includes attachments in the count. If you click on the number in that column, only posts and pages are displayed. There are similar issues with custom post types and taxonomies (whether you use the Assistant or not). The “Attachments” column in the edit screens added by the Assistant shows the correct count because it works in a different way.

    This is still an issue; Trac ticket #14084 has attracted 32 comments, the most recent on 3/2/2013. I haven’t been able to reproduce the problem in a controlled way, but I’m confident it is not related to the MLA plugin. I wish there was a better answer, but the fix will be in WordPress, not the plugin. For that reason, I’m going to mark this issue resolved.

    I managed to fix the problem by adding this (extremely hack-y) code into my theme’s functions.php file:

    function correct_att_tag_count($att_id) {
    		if ( !current_user_can( 'edit_posts', $att_id) ) {
    	global $wpdb;
    	$att_tags = $wpdb->get_results( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = 'attachment_tag' " );
    	foreach( $att_tags as $tag ) {
    		$tag->count = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->term_relationships WHERE term_taxonomy_id = $tag->term_taxonomy_id " );
    		$wpdb->query( "UPDATE $wpdb->term_taxonomy SET count = $tag->count WHERE term_taxonomy_id = $tag->term_taxonomy_id");
    add_action( 'edit_attachment', 'correct_att_tag_count');

    This has fixed the immediate problem with the ‘count’ field of the term_taxonomy database, which is used by WordPress functions such as wp_tag_cloud. It’s rather inefficient as it uses 2 SQL queries iterating through every tag (not just the ones that need updating) although it works well in my non-robust tests. There should probably be a nonce check, but I couldn’t find the correct method to do so for terms.

    Anyway, if anyone needs it… this is a good start.

    Plugin Author David Lingren



    Thanks for contributing this work-around. I just checked WordPress Trac, and it looks like they have closed ticket #14084 after three years and 32 comments:

    Custom taxonomy count includes draft & trashed posts

    Note the mention of “draft & trashed posts” in the ticket title. I don’t see anything in your code that checks for these conditions, so the count you calculate may not agree with the “official” WordPress definition of what the count is supposed to include.

    Also, if you use the same taxonomy, e.g., Categories or Tags, for both attachments and posts/pages, you will include the attachments in the “Posts” column values for Posts and Pages; also different from the official WordPress logic.

    It’s a mess, which is why Media Library Assistant doesn’t rely on the field for its “Attachments” column.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Incorrect Att. Tags Count Saved to term_taxonomy Table’ is closed to new replies.