Opened 5 months ago
Last modified 3 days ago
#23044 new enhancement
adjacent_image_link() needs optimization
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 3.6 |
| Component: | Query | Version: | 3.5 |
| Severity: | major | Keywords: | |
| Cc: | xoodrew@…, ADAMSILVERSTEIN@…, dromsey@…, hardyy |
Description
adjacent_image_link() is really slow and stupid. It queries for all of the images attached to that post. And then it picks the previous or next one in PHP. And there's no caching, so you'll typically get this happening three times on an attachment page, (image [linked to next], prev link, next link).
We should actually just make the query grab the ONE image we want.
Attachments (1)
Change History (10)
comment:1
markjaquith — 5 months ago
comment:3
DrewAPicture — 5 months ago
- Cc xoodrew@… added
comment:4
adamsilverstein — 5 months ago
- Cc ADAMSILVERSTEIN@… added
comment:6
SergeyBiryukov — 4 months ago
- Version changed from trunk to 3.5
Added an optional argument in get_children() so that it returns numerical array containing only IDs of elements. Easier to return than array of objects.
Also makes it easier to search through in-built array functions.
in_array() is added to make sure loop don't skip in some cases.
If i have done any mistake, feel free to notify me.
As well as the major optimisation of an improved query here there is a simple optimisation we could use to improve the performance of this on sites with a large number of images attached to a post.
If we switch from get_children to get_posts we can avoid calling update_post_cache on every attachment post for every query.

Talked with Nacin about this. This may end up being moot if we solve the bigger issue of determining the correct prev/next image, considering that images don't have to be in parent-post-bounded galleries.
In the meantime, this is the plugin I wrote to make it so that it doesn't query everything:
https://gist.github.com/4357848