wp_queue_posts_for_term_meta_lazyload

The timeline below displays how wordpress function wp_queue_posts_for_term_meta_lazyload has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Queues posts for lazy-loading of term meta.
 *
 * @since 4.5.0
 *
 * @param WP_Post[] $posts Array of WP_Post objects.
 */
function wp_queue_posts_for_term_meta_lazyload($posts)
{
    $post_type_taxonomies = array();
    $prime_post_terms = array();
    foreach ($posts as $post) {
        if (!$post instanceof WP_Post) {
            continue;
        }
        if (!isset($post_type_taxonomies[$post->post_type])) {
            $post_type_taxonomies[$post->post_type] = get_object_taxonomies($post->post_type);
        }
        foreach ($post_type_taxonomies[$post->post_type] as $taxonomy) {
            $prime_post_terms[$taxonomy][] = $post->ID;
        }
    }
    $term_ids = array();
    if ($prime_post_terms) {
        foreach ($prime_post_terms as $taxonomy => $post_ids) {
            $cached_term_ids = wp_cache_get_multiple($post_ids, "{$taxonomy}_relationships");
            if (is_array($cached_term_ids)) {
                $cached_term_ids = array_filter($cached_term_ids);
                foreach ($cached_term_ids as $_term_ids) {
                    // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.
                    foreach ($_term_ids as $term_id) {
                        if (is_numeric($term_id)) {
                            $term_ids[] = (int) $term_id;
                        } elseif (isset($term_id->term_id)) {
                            $term_ids[] = (int) $term_id->term_id;
                        }
                    }
                }
            }
        }
        $term_ids = array_unique($term_ids);
    }
    wp_lazyload_term_meta($term_ids);
}

WordPress Version: 6.2

/**
 * Queues posts for lazy-loading of term meta.
 *
 * @since 4.5.0
 *
 * @param WP_Post[] $posts Array of WP_Post objects.
 */
function wp_queue_posts_for_term_meta_lazyload($posts)
{
    $post_type_taxonomies = array();
    $prime_post_terms = array();
    foreach ($posts as $post) {
        if (!$post instanceof WP_Post) {
            continue;
        }
        if (!isset($post_type_taxonomies[$post->post_type])) {
            $post_type_taxonomies[$post->post_type] = get_object_taxonomies($post->post_type);
        }
        foreach ($post_type_taxonomies[$post->post_type] as $taxonomy) {
            $prime_post_terms[$taxonomy][] = $post->ID;
        }
    }
    $term_ids = array();
    if ($prime_post_terms) {
        $prime_term_ids = array();
        $prime_taxonomy_ids = array();
        foreach ($prime_post_terms as $taxonomy => $post_ids) {
            $cached_term_ids = wp_cache_get_multiple($post_ids, "{$taxonomy}_relationships");
            if (is_array($cached_term_ids)) {
                $cached_term_ids = array_filter($cached_term_ids);
                foreach ($cached_term_ids as $_term_ids) {
                    // Backward compatibility for if a plugin is putting objects into the cache, rather than IDs.
                    foreach ($_term_ids as $term_id) {
                        if (is_numeric($term_id)) {
                            $prime_term_ids[] = (int) $term_id;
                            $prime_taxonomy_ids[$taxonomy][] = (int) $term_id;
                        } elseif (isset($term_id->term_id)) {
                            $prime_taxonomy_ids[$taxonomy][] = (int) $term_id->term_id;
                            $prime_term_ids[] = (int) $term_id->term_id;
                        }
                    }
                }
            }
        }
        if ($prime_term_ids) {
            $prime_term_ids = array_unique($prime_term_ids);
            // Do not prime term meta at this point, let the lazy loader take care of that.
            _prime_term_caches($prime_term_ids, false);
            foreach ($prime_taxonomy_ids as $taxonomy => $_term_ids) {
                foreach ($_term_ids as $term_id) {
                    if (in_array($term_id, $term_ids, true)) {
                        continue;
                    }
                    $term = get_term($term_id, $taxonomy);
                    if (is_wp_error($term)) {
                        continue;
                    }
                    $term_ids[] = $term_id;
                }
            }
        }
    }
    if ($term_ids) {
        $lazyloader = wp_metadata_lazyloader();
        $lazyloader->queue_objects('term', $term_ids);
    }
}

WordPress Version: 6.1

/**
 * Queues posts for lazy-loading of term meta.
 *
 * @since 4.5.0
 *
 * @param WP_Post[] $posts Array of WP_Post objects.
 */
function wp_queue_posts_for_term_meta_lazyload($posts)
{
    $post_type_taxonomies = array();
    $term_ids = array();
    foreach ($posts as $post) {
        if (!$post instanceof WP_Post) {
            continue;
        }
        if (!isset($post_type_taxonomies[$post->post_type])) {
            $post_type_taxonomies[$post->post_type] = get_object_taxonomies($post->post_type);
        }
        foreach ($post_type_taxonomies[$post->post_type] 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 (!in_array($term->term_id, $term_ids, true)) {
                        $term_ids[] = $term->term_id;
                    }
                }
            }
        }
    }
    if ($term_ids) {
        $lazyloader = wp_metadata_lazyloader();
        $lazyloader->queue_objects('term', $term_ids);
    }
}

WordPress Version: 5.7

/**
 * Queues posts for lazy-loading of term meta.
 *
 * @since 4.5.0
 *
 * @param array $posts Array of WP_Post objects.
 */
function wp_queue_posts_for_term_meta_lazyload($posts)
{
    $post_type_taxonomies = array();
    $term_ids = array();
    foreach ($posts as $post) {
        if (!$post instanceof WP_Post) {
            continue;
        }
        if (!isset($post_type_taxonomies[$post->post_type])) {
            $post_type_taxonomies[$post->post_type] = get_object_taxonomies($post->post_type);
        }
        foreach ($post_type_taxonomies[$post->post_type] 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 (!in_array($term->term_id, $term_ids, true)) {
                        $term_ids[] = $term->term_id;
                    }
                }
            }
        }
    }
    if ($term_ids) {
        $lazyloader = wp_metadata_lazyloader();
        $lazyloader->queue_objects('term', $term_ids);
    }
}

WordPress Version: 5.3

/**
 * Queues posts for lazy-loading of term meta.
 *
 * @since 4.5.0
 *
 * @param array $posts Array of WP_Post objects.
 */
function wp_queue_posts_for_term_meta_lazyload($posts)
{
    $post_type_taxonomies = array();
    $term_ids = array();
    foreach ($posts as $post) {
        if (!$post instanceof WP_Post) {
            continue;
        }
        if (!isset($post_type_taxonomies[$post->post_type])) {
            $post_type_taxonomies[$post->post_type] = get_object_taxonomies($post->post_type);
        }
        foreach ($post_type_taxonomies[$post->post_type] 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;
                    }
                }
            }
        }
    }
    if ($term_ids) {
        $lazyloader = wp_metadata_lazyloader();
        $lazyloader->queue_objects('term', $term_ids);
    }
}

WordPress Version: 4.5

/**
 * Queues posts for lazy-loading of term meta.
 *
 * @since 4.5.0
 *
 * @param array $posts Array of WP_Post objects.
 */
function wp_queue_posts_for_term_meta_lazyload($posts)
{
    $post_type_taxonomies = $term_ids = array();
    foreach ($posts as $post) {
        if (!$post instanceof WP_Post) {
            continue;
        }
        if (!isset($post_type_taxonomies[$post->post_type])) {
            $post_type_taxonomies[$post->post_type] = get_object_taxonomies($post->post_type);
        }
        foreach ($post_type_taxonomies[$post->post_type] 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;
                    }
                }
            }
        }
    }
    if ($term_ids) {
        $lazyloader = wp_metadata_lazyloader();
        $lazyloader->queue_objects('term', $term_ids);
    }
}