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

wp query - How do I force WP_query to fetch fresh, non-cached results?

programmeradmin4浏览0评论

Context: In a social sharing plugin that I work on, I'm store social share counts in hidden post meta (custom fields) so that I can use that data for various functions around the plugin.

One useful use of this data is to create a Popular Posts widget that is based on the number of social shares that a post has. I use a query with the following arguments to accomplish this:

Example Code:

        $swp_args = array(
            'posts_per_page'    => $count,
            'post_type'         => 'post',
            'meta_key'          => '_' . $network,
            'orderby'           => 'meta_value_num',
            'order'             => 'DESC',
        );
        $swq = new WP_Query( $swp_args );

Where $count is a valid integer and $network is a valid name of one of the meta fields associated with the share count of a particular social network or the _totes field which reflects total shares across all networks.

The Problem: On 99% of sites, this query works spendidly and people think it's just the bees knees. However, on a small handful of sites, I've discovered some unusual behavior. In some instances, the widget never seems to update. For example, on the post itself, the post will reflect that it has 100 shares, but the widget will remain at 80 shares and continue to sort according to 80 shares. Both the post and the widget are using the exact same meta field for these functions.

The Question: What would cause the WP_query to continue returning outdated data? If it is some sort of caching, is there a way to force the query to not be cached but to always return fresh data from the database?

Context: In a social sharing plugin that I work on, I'm store social share counts in hidden post meta (custom fields) so that I can use that data for various functions around the plugin.

One useful use of this data is to create a Popular Posts widget that is based on the number of social shares that a post has. I use a query with the following arguments to accomplish this:

Example Code:

        $swp_args = array(
            'posts_per_page'    => $count,
            'post_type'         => 'post',
            'meta_key'          => '_' . $network,
            'orderby'           => 'meta_value_num',
            'order'             => 'DESC',
        );
        $swq = new WP_Query( $swp_args );

Where $count is a valid integer and $network is a valid name of one of the meta fields associated with the share count of a particular social network or the _totes field which reflects total shares across all networks.

The Problem: On 99% of sites, this query works spendidly and people think it's just the bees knees. However, on a small handful of sites, I've discovered some unusual behavior. In some instances, the widget never seems to update. For example, on the post itself, the post will reflect that it has 100 shares, but the widget will remain at 80 shares and continue to sort according to 80 shares. Both the post and the widget are using the exact same meta field for these functions.

The Question: What would cause the WP_query to continue returning outdated data? If it is some sort of caching, is there a way to force the query to not be cached but to always return fresh data from the database?

Share Improve this question edited Jul 10, 2017 at 21:00 Den Isahac 9637 silver badges20 bronze badges asked Jul 10, 2017 at 20:04 Nicholas CardotNicholas Cardot 1851 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

WP_Query has a Caching Parameters you can configure to return the desired data with/without caching.

$swp_args = array(
    // Show posts without adding post meta information to the cache.
    'update_post_meta_cache' => false,
    ...
);

In your specific case update_post_meta_cache allows you to turn on/off the caching for post meta.

发布评论

评论列表(0)

  1. 暂无评论