10up / Engineering-Best-Practices

10up Engineering Best Practices
https://10up.github.io/Engineering-Best-Practices/
MIT License
759 stars 205 forks source link

'hierarchical => false' When Using WP_Term_Query on Large Taxonomy #199

Open bengreeley opened 7 years ago

bengreeley commented 7 years ago

Functions that use the WP_Term_Query class such as get_terms() have hierarchical => true set as a default. This can be problematic for taxonomies that have large number of nested terms. When hierarchical is set to true, the class calls get_term_children(), which in turn calls itself recursively. For instances where there is a small number of terms (less than 1000, it seems) this isn't an issue, but on a large number of nested terms, thousands of additional queries are run, which can slow pages considerably.

On one project a taxonomy was set up as nested terms in a 'location' taxonomy. This included Country -> State -> County -> City . The number of terms was somewhere around 60,000, which slowed archive pages to a crawl as it parsed all of those terms recursively with new queries.

I haven't determined the limits for a recommended threshold for depth or number of terms that would result in slower queries, but I'd like to propose we mention in the best practices to avoid using hierarchical => true on large or deeply nested taxonomies until this recursive functionality is replaced in core. Curious what others think.

tlovett1 commented 7 years ago

@bengreeley appreciate the post. I took a brief look at the get_terms method in WP_Term_Query and see what you're saying. However, the function is pretty dense (800+ lines long) so it was hard to follow.

I did see some caching in _get_term_hierarchy which is called by get_term_children.

I'd love to see a PR written with this. I also think it would be worth addressing this in core.

ganusoft commented 6 years ago

I am working on a project that sale car parts and i have created year/make/model with Toolset taxonomy but after i entered all the cars and models it has become very slow to load the shop page. I trying to following post but i am not too good in wordpress. Please is there anyway you can assist me.

ericmann commented 6 years ago

@ganusoft This is not a support forum, it's an issue tracker for best practices documentation regarding WordPress development in general. If you're looking for direct user support, I'd encourage you to visit the official WordPress support forum.