update_post_caches

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

WordPress Version: 6.1

/**
 * Updates post, term, and metadata caches for a list of post objects.
 *
 * @since 1.5.0
 *
 * @param WP_Post[] $posts             Array of post objects (passed by reference).
 * @param string    $post_type         Optional. Post type. Default 'post'.
 * @param bool      $update_term_cache Optional. Whether to update the term cache. Default true.
 * @param bool      $update_meta_cache Optional. Whether to update the meta cache. Default true.
 */
function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true)
{
    // No point in doing all this work if we didn't match any posts.
    if (!$posts) {
        return;
    }
    update_post_cache($posts);
    $post_ids = array();
    foreach ($posts as $post) {
        $post_ids[] = $post->ID;
    }
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($update_term_cache) {
        if (is_array($post_type)) {
            $ptypes = $post_type;
        } elseif ('any' === $post_type) {
            $ptypes = array();
            // Just use the post_types in the supplied posts.
            foreach ($posts as $post) {
                $ptypes[] = $post->post_type;
            }
            $ptypes = array_unique($ptypes);
        } else {
            $ptypes = array($post_type);
        }
        if (!empty($ptypes)) {
            update_object_term_cache($post_ids, $ptypes);
        }
    }
    if ($update_meta_cache) {
        update_postmeta_cache($post_ids);
    }
}

WordPress Version: 5.5

/**
 * Call major cache updating functions for list of Post objects.
 *
 * @since 1.5.0
 *
 * @param WP_Post[] $posts             Array of Post objects
 * @param string    $post_type         Optional. Post type. Default 'post'.
 * @param bool      $update_term_cache Optional. Whether to update the term cache. Default true.
 * @param bool      $update_meta_cache Optional. Whether to update the meta cache. Default true.
 */
function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true)
{
    // No point in doing all this work if we didn't match any posts.
    if (!$posts) {
        return;
    }
    update_post_cache($posts);
    $post_ids = array();
    foreach ($posts as $post) {
        $post_ids[] = $post->ID;
    }
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($update_term_cache) {
        if (is_array($post_type)) {
            $ptypes = $post_type;
        } elseif ('any' === $post_type) {
            $ptypes = array();
            // Just use the post_types in the supplied posts.
            foreach ($posts as $post) {
                $ptypes[] = $post->post_type;
            }
            $ptypes = array_unique($ptypes);
        } else {
            $ptypes = array($post_type);
        }
        if (!empty($ptypes)) {
            update_object_term_cache($post_ids, $ptypes);
        }
    }
    if ($update_meta_cache) {
        update_postmeta_cache($post_ids);
    }
}

WordPress Version: 5.3

/**
 * Call major cache updating functions for list of Post objects.
 *
 * @since 1.5.0
 *
 * @param WP_Post[] $posts             Array of Post objects
 * @param string    $post_type         Optional. Post type. Default 'post'.
 * @param bool      $update_term_cache Optional. Whether to update the term cache. Default true.
 * @param bool      $update_meta_cache Optional. Whether to update the meta cache. Default true.
 */
function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true)
{
    // No point in doing all this work if we didn't match any posts.
    if (!$posts) {
        return;
    }
    update_post_cache($posts);
    $post_ids = array();
    foreach ($posts as $post) {
        $post_ids[] = $post->ID;
    }
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($update_term_cache) {
        if (is_array($post_type)) {
            $ptypes = $post_type;
        } elseif ('any' == $post_type) {
            $ptypes = array();
            // Just use the post_types in the supplied posts.
            foreach ($posts as $post) {
                $ptypes[] = $post->post_type;
            }
            $ptypes = array_unique($ptypes);
        } else {
            $ptypes = array($post_type);
        }
        if (!empty($ptypes)) {
            update_object_term_cache($post_ids, $ptypes);
        }
    }
    if ($update_meta_cache) {
        update_postmeta_cache($post_ids);
    }
}

WordPress Version: 4.2

/**
 * Call major cache updating functions for list of Post objects.
 *
 * @since 1.5.0
 *
 * @param array  $posts             Array of Post objects
 * @param string $post_type         Optional. Post type. Default 'post'.
 * @param bool   $update_term_cache Optional. Whether to update the term cache. Default true.
 * @param bool   $update_meta_cache Optional. Whether to update the meta cache. Default true.
 */
function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true)
{
    // No point in doing all this work if we didn't match any posts.
    if (!$posts) {
        return;
    }
    update_post_cache($posts);
    $post_ids = array();
    foreach ($posts as $post) {
        $post_ids[] = $post->ID;
    }
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($update_term_cache) {
        if (is_array($post_type)) {
            $ptypes = $post_type;
        } elseif ('any' == $post_type) {
            $ptypes = array();
            // Just use the post_types in the supplied posts.
            foreach ($posts as $post) {
                $ptypes[] = $post->post_type;
            }
            $ptypes = array_unique($ptypes);
        } else {
            $ptypes = array($post_type);
        }
        if (!empty($ptypes)) {
            update_object_term_cache($post_ids, $ptypes);
        }
    }
    if ($update_meta_cache) {
        update_postmeta_cache($post_ids);
    }
}

WordPress Version: 4.0

/**
 * Call major cache updating functions for list of Post objects.
 *
 * @since 1.5.0
 *
 * @param array  $posts             Array of Post objects
 * @param string $post_type         Optional. Post type. Default 'post'.
 * @param bool   $update_term_cache Optional. Whether to update the term cache. Default true.
 * @param bool   $update_meta_cache Optional. Whether to update the meta cache. Default true.
 */
function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true)
{
    // No point in doing all this work if we didn't match any posts.
    if (!$posts) {
        return;
    }
    update_post_cache($posts);
    $post_ids = array();
    foreach ($posts as $post) {
        $post_ids[] = $post->ID;
    }
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($update_term_cache) {
        if (is_array($post_type)) {
            $ptypes = $post_type;
        } elseif ('any' == $post_type) {
            // Just use the post_types in the supplied posts.
            foreach ($posts as $post) {
                $ptypes[] = $post->post_type;
            }
            $ptypes = array_unique($ptypes);
        } else {
            $ptypes = array($post_type);
        }
        if (!empty($ptypes)) {
            update_object_term_cache($post_ids, $ptypes);
        }
    }
    if ($update_meta_cache) {
        update_postmeta_cache($post_ids);
    }
}

WordPress Version: 3.9

/**
 * Call major cache updating functions for list of Post objects.
 *
 * @since 1.5.0
 *
 * @uses update_post_cache()
 * @uses update_object_term_cache()
 * @uses update_postmeta_cache()
 *
 * @param array $posts Array of Post objects
 * @param string $post_type The post type of the posts in $posts. Default is 'post'.
 * @param bool $update_term_cache Whether to update the term cache. Default is true.
 * @param bool $update_meta_cache Whether to update the meta cache. Default is true.
 */
function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true)
{
    // No point in doing all this work if we didn't match any posts.
    if (!$posts) {
        return;
    }
    update_post_cache($posts);
    $post_ids = array();
    foreach ($posts as $post) {
        $post_ids[] = $post->ID;
    }
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($update_term_cache) {
        if (is_array($post_type)) {
            $ptypes = $post_type;
        } elseif ('any' == $post_type) {
            // Just use the post_types in the supplied posts.
            foreach ($posts as $post) {
                $ptypes[] = $post->post_type;
            }
            $ptypes = array_unique($ptypes);
        } else {
            $ptypes = array($post_type);
        }
        if (!empty($ptypes)) {
            update_object_term_cache($post_ids, $ptypes);
        }
    }
    if ($update_meta_cache) {
        update_postmeta_cache($post_ids);
    }
}

WordPress Version: 3.7

/**
 * Call major cache updating functions for list of Post objects.
 *
 * @package WordPress
 * @subpackage Cache
 * @since 1.5.0
 *
 * @uses update_post_cache()
 * @uses update_object_term_cache()
 * @uses update_postmeta_cache()
 *
 * @param array $posts Array of Post objects
 * @param string $post_type The post type of the posts in $posts. Default is 'post'.
 * @param bool $update_term_cache Whether to update the term cache. Default is true.
 * @param bool $update_meta_cache Whether to update the meta cache. Default is true.
 */
function update_post_caches(&$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true)
{
    // No point in doing all this work if we didn't match any posts.
    if (!$posts) {
        return;
    }
    update_post_cache($posts);
    $post_ids = array();
    foreach ($posts as $post) {
        $post_ids[] = $post->ID;
    }
    if (!$post_type) {
        $post_type = 'any';
    }
    if ($update_term_cache) {
        if (is_array($post_type)) {
            $ptypes = $post_type;
        } elseif ('any' == $post_type) {
            // Just use the post_types in the supplied posts.
            foreach ($posts as $post) {
                $ptypes[] = $post->post_type;
            }
            $ptypes = array_unique($ptypes);
        } else {
            $ptypes = array($post_type);
        }
        if (!empty($ptypes)) {
            update_object_term_cache($post_ids, $ptypes);
        }
    }
    if ($update_meta_cache) {
        update_postmeta_cache($post_ids);
    }
}