最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Including post thumbnail url in loop's post object to reduce number of queries

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited May 16, 2019 at 8:15 bueltge 17.1k7 gold badges62 silver badges97 bronze badges asked May 15, 2019 at 21:13 TheDraomTheDraom 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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/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!

发布评论

评论列表(0)

  1. 暂无评论