- /*
- * We only do this once per `WP_Query` instance.
- * Can't use `remove_filter()` because of non-unique object hashes.
- */
- if ( $this->updated_term_meta_cache ) {
- return $check;
- }
-
- // We can only lazyload if the entire post object is present.
- $posts = array();
- foreach ( $this->posts as $post ) {
- if ( $post instanceof WP_Post ) {
- $posts[] = $post;
- }
- }
-
- if ( ! empty( $posts ) ) {
- // Fetch cached term_ids for each post. Keyed by term_id for faster lookup.
- $term_ids = array();
- foreach ( $posts as $post ) {
- $taxonomies = get_object_taxonomies( $post->post_type );
- foreach ( $taxonomies as $taxonomy ) {
- // Term cache should already be primed by 'update_post_term_cache'.
- $terms = get_object_term_cache( $post->ID, $taxonomy );
- if ( false !== $terms ) {
- foreach ( $terms as $term ) {
- if ( ! isset( $term_ids[ $term->term_id ] ) ) {
- $term_ids[ $term->term_id ] = 1;
- }
- }
- }
- }
- }
-
- /*
- * Only update the metadata cache for terms belonging to these posts if the term_id passed
- * to `get_term_meta()` matches one of those terms. This prevents a single call to
- * `get_term_meta()` from priming metadata for all `WP_Query` objects.
- */
- if ( isset( $term_ids[ $term_id ] ) ) {
- update_termmeta_cache( array_keys( $term_ids ) );
- $this->updated_term_meta_cache = true;
- }
- }
-
- // If no terms were found, there's no need to run this again.
- if ( empty( $term_ids ) ) {
- $this->updated_term_meta_cache = true;
- }
-