Based on @Mikes suggestion of what was happening (post attach_count being incorrectly set by add-on, and subsequently decremented to 0), I created the following query to detect where the actual attachment count for a given post_id was different than the post's attach_count:
Code:
SELECT post.post_id, post.attach_count, attachment.count
FROM xf_post as post
INNER JOIN (SELECT content_id, COUNT(content_id) AS count FROM xf_attachment WHERE content_type = 'post' GROUP BY content_id) attachment ON post.post_id = attachment.content_id
WHERE post.attach_count <> attachment.count
Based on analyzing the count mismatches for my recent posts, and knowing what edits I had made to them, I was able to figure out the pattern - the attach_count was equal to the the number of attachments added by Convert Image
in the last edit, rather than the total number of attachments. E.g., if you created a post with 3 hotlinked images, Convert Image would create three attachments, and correctly set the post's attach_count to 3. However, if you edited this post and added 1 additional hotlinked image, Convert Image would incorrectly set the attach_count to 1, instead of adding 1 to the existing attach_count (total of 4). If you then subsequently deleted 1 attachment, you would have an attach_count of 0, and images would not be displayed.
I happened to hit this bug
a lot due to a specific workflow where I use the RSS feed importer to create posts, which typically have multiple hotlinked images, which Convert Image correctly updated the attach_count for, then I usually edit the post to add a missing "hero" image to the post, at which point Convert Image would set the attach_count to 1, then I often edit the post again to remove 1 pixel invisible tracking images, advertisements, etc.
I have provided the root cause to
@AndyB, who has updated Convert Image and Convert Image All XF 1.5 and XF 2.0 add-ons to fix this issue going forward.
Maybe a query can be used to correct this?
You can fix existing attach_count mismatches running the following SQL script in phpMyAdmin, which I have tested on my forum:
Code:
UPDATE xf_post AS post
INNER JOIN (SELECT content_id, COUNT(content_id) AS count FROM xf_attachment
WHERE content_type = 'post'
GROUP BY content_id) attachment ON post.post_id = attachment.content_id
SET post.attach_count = attachment.count
WHERE post.attach_count <> attachment.count
DISCLAIMER: You should backup your site and database before running this script and I am not responsible for any bad things that may happen.