2

My theme runs a loop that does not require post terms to show. However Wordpress core runs the wp_get_object_terms function on every iteration.

This results in unnecessary DB queries such as:

SELECT t.*, tt.*
FROM wp_terms AS t
INNER JOIN wp_term_taxonomy AS tt
ON tt.term_id = t.term_id
INNER JOIN wp_term_relationships AS tr
ON tr.term_taxonomy_id = tt.term_taxonomy_id
WHERE tt.taxonomy IN ('category')
AND tr.object_id IN (81595)
ORDER BY t.name ASC

Is there a way to prevent / reduce or optimize the calling of wp_get_object_terms() to only when it is absolutely necessary?

I've tracked it down to get_permalink() which is called within the loop. Our site uses permalinks that include the category, which explains why it is searching for the category for every post. Would there be any way to optimize this?

  • Why do you think it is coming from core code? Have you narrowed it down to specific place it is called from? – Rarst May 9 '15 at 19:37
  • @Rarst, I've "grepped" through the theme as well as the plugins, which don't bring it up. I've also used the "Query Monitor" plugin which indicates that it is from core, as the theme files / functions don't use it. – Josh May 9 '15 at 20:28
  • That is not conclusive. WP API calls can be highly nested, the fact that this specific function isn't called in extension space doesn't mean neither are many other things that use it. The specific call might be coming from core, but it also might not. – Rarst May 9 '15 at 20:49
  • Yes but, regardless of whether it is coming from core or not, is there a way to rewrite the query loop such that it isn't called? I have around 50 DB queries on page load just as a result of this. – Josh May 10 '15 at 10:54
  • 1
    Ok I've tracked it down to get_permalink() which is called within the loop. Our site uses permalinks that include the category, which explains why it is searching for the category for every post. Would there be any way to optimize this? – Josh May 11 '15 at 17:25
2

As you had narrowed it down, since your post permalinks include category WP is trying to figure out how to put them together for every post.

Object Cache

The staple approach would be to optimize efficiency of overall setup, rather than micromanage generations of bits and pieces. If you set up persistent Object Cache for the site, then category information would be transparently cached in memory, rather than retrieved from database every time.

Shortlinks

If that is not an option or you are generating links at a scale that even gives object caching an issue there is a slightly hacky alternative. While you have "pretty" links enabled it doesn't mean that "not pretty" links stopped working.

You can use wp_get_shortlink() function to generate latter kind of links (/?p=id), which would be much lighter on resources.

They won't look nice in source, but when visited WP will redirect them to "pretty" versions transparently. Of course excessive redirects aren't a good thing either, but it's up to you if the trade-off is meaningful for your circumstances.

  • Thanks for this suggestion. We will be implementing object cache. Shortlinks are a nice suggestion, but what would their effect be on SEO? Also will the server not be redirecting anyway when the user clicks, so will it really have much effect on performance? Considering we have something like 25'000 posts. – Josh May 12 '15 at 8:57
  • SEO effects are not my area. There will be extra redirest from shortlink to pretty permalink. Just presenting you with an option, it's up to you if it would make sense. As I said it's on a hacky side, Object Cache is a necessity for any reasonably sized site anyway. – Rarst May 12 '15 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.