1

I'm working now on high performance Wordpress website and it requires me to analyze a lot of queries, caching options etc. I've discovered that if you use get_the_post_thumbnail() even within the loop, then WordPress makes a query for every post's thumbnail url.

So if you've got your page set to display 25 posts with thumbnails, it makes 25 additional queries to retrieve image URL.

Without using get_the_post_thumbnail() I'm reducing query number from 49 to 4. A big number. However, thumbnails are really needed for this theme.

Do you know if there's any way to include post thumbnail URL in the post object by default?

1 Answer 1

0

I believe you can fetch that data using setup_postdata() which lets you access all the data relating to a post and use it with standard template tags (in your case, the_post_thumbnail).

There is a great tutorial here: https://wpshout.com/setup_postdata-template-tags-need-custom-wp_post-arrays-crave/

Important to note that you first have to setup the global $post and you have to be sure to reset using wp_reset_postdata at the end of your query.

I hope this works for you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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