quick bug(?) report – caching & switch_to_blog
-
I’m on deadline so I haven’t been able to make an isolated case, but wanted to report a bug I saw…
On a support page (admin-only, not in the normal user flow) I’m doing a loop over every blog in a multi-site installation; for each I switch_to_blog and fetch N random posts, and from each of those, display the first image attachment.
Basic logic:
foreach (get_blog_list( 0, 'all' ) AS $blog) { switch_to_blog($blog['blog_id']); foreach(get_posts('numberposts=25') as $post) { $att = get_posts("...post_parent=$post->ID,etc...")[0]; $meta = wp_get_attachment_metadata($att->ID); } restore_current_blog(); }
What I found was that if two attachments (from different blogs) had the same ID, when I called wp_get_attachment_metadata the metadata for the first instance was returned for the second, even though I’d done a switch_to_blog.
I settled on the nuclear option. Adding a call to wp_cache_flush() after the switch_to_blog fixed the problem for me, and as this is a very infrequently used page, that’s sufficient for my needs. But it indicates to me that there’s some cache that’s not being cleared properly.
I only traced into the caching as far as get_metadata(), and it seems unlikely to me that you’d miss something as big as the metadata caches in switch_to_blog, so I’m suspecting that either I’m doing something funny or that some plugin is hooked into the cache and causing problems. (I -am- running w3 total cache)
Anyway, just wanted to toss it out there in case others see a similar issue.
- The topic ‘quick bug(?) report – caching & switch_to_blog’ is closed to new replies.