• Resolved Vaclav Elias

    (@vaclavelias)


    Hi,

    It seems that your plugin, specifically function wpmediacategory_update_count_callback is overriding the correct default Count of posts.

    Your query is including post revisions. For example I have got 3 posts in Category A, which I edited a few times.

    When I add an image (through your plugin) to a category B, you are looping through all categories and updating total which is found in term_relationships but you don’t check that some of those are actually not published posts but revisions.

    I didn’t go deeper just updated your query to get the numbers right, but I don’t count actual images in categories as they are now excluded due to my selection p.post_status=’publish’.

    You would need to:

    a) ideally, don’t override all categories, only the one which was updated with a new image or image removed and make sure to exclude revisions or
    b) if you loop and update all categories make sure you are not counting revisions for posts

    SELECT term_taxonomy_id, MAX(total) AS total FROM ((
    	SELECT tt.term_taxonomy_id, COUNT(*) AS total 
        FROM wp_term_relationships tr, wp_term_taxonomy tt <strong>, wp_posts p</strong>
        WHERE tr.term_taxonomy_id = tt.term_taxonomy_id <strong>AND tr.object_id = p.ID</strong> AND tt.taxonomy = 'category' <strong>AND p.post_status='publish'</strong> GROUP BY tt.term_taxonomy_id
    	) UNION ALL (
    	SELECT term_taxonomy_id, 0 AS total FROM wp_term_taxonomy WHERE taxonomy = 'category'
    	)) AS unioncount GROUP BY term_taxonomy_id

    The only way at the moment to get the correct Count back in categories, is to disable your plugin, and update one random post in each category to get the numbers correct.

    • This topic was modified 8 years, 3 months ago by Vaclav Elias.
    • This topic was modified 8 years, 3 months ago by Vaclav Elias.
    • This topic was modified 8 years, 3 months ago by Vaclav Elias.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Vaclav Elias

    (@vaclavelias)

    Note, that my query is just an example..

    Plugin Author jeffrey-wp

    (@jeffrey-wp)

    Hi Vaclav,

    Are you using any other plugins besides mine that uses categories? I can’t reproduce your situation. I’ve tested with the default WordPress categories and with custom categories/taxonomies bought ways show a correct count of the posts (even with revisions).

    The table wp_term_relationships (which is used for counting the number of posts assigned to a category) only stores the terms (categories) from the published posts / media (not the revisions).

    Thread Starter Vaclav Elias

    (@vaclavelias)

    Hi jeffrey,

    Hmm, I would need to investigate further. I don’t have any other plugin that uses categories, just yours.

    When I add an existing image to category (through your plugin) in Media it messes the category counting.

    What you say is true, that table wp_term_relationships only stores terms from the published posts but your code is looping through all categories and updating total but your code doesn’t check that some of those are actually not published posts but revisions.

    Let me do a few more tests and I will let you know ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Bug in Category Count – wpmediacategory_update_count_callback’ is closed to new replies.