Make WordPress Core

Opened 7 years ago

Closed 6 years ago

#27572 closed enhancement (duplicate)

get_post_format_link and db queries

Reported by: afercia Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.8
Component: Post Formats Keywords:
Focuses: Cc:


many themes, including Twenty Fourteen, make use of get_post_format_link() to display a link to a post format index (e.g. /type/video/)

get_post_format_link() makes use of get_term_by() which, as far as I see, always hits the database to get the post_format term object, then it passes that term object to get_term_link().

When inside the loop, you likely want to display a link to the post format index *of the current post*, no need to pass a $format string and the post_format term object is already cached and available in $wp_object_cache, so why hit the database?

Imagine an archive page where you display 10 posts and none of them is a "standard" post: you get 10 extra (useless) queries (tested on Twenty Fourteen).

IMHO the only use case for get_post_format_link is when you're *outside* the loop, so you pass the post format slug and you get your link at a cost of 1 additional query.
But when inside the loop, it would be great to save some queries.

If it sounds sensible for you, I would greatly appreciate your feedback, didn't attach a patch because I don't know if you would prefer to refactor get_post_format_link() trying to keep backwards compatibility or build a new function and encourage to use that instead.

Just as an example, you may want to have a look at the attached new function, largely based on the way get_post_format() gets what it needs from the cache.

Attachments (1)

my_get_post_format_link.php (596 bytes) - added by afercia 7 years ago.

Download all attachments as: .zip

Change History (3)

#1 @SergeyBiryukov
7 years ago

  • Version changed from trunk to 3.8

#2 @dd32
6 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

I'm closing this as a duplicate of #21760 as it takes what I think, is a better route here.

Rather than introducing a new function, or making get_post_format_link() reliant upon the current post, it adds caching to the get_term_by() function which gives a far better developer experience. Additionally, if the term is attached to an existing post in the current query, it should be automatically pulled into the cache on view by update_post_caches().

Note: See TracTickets for help on using tickets.