• Resolved aldiaa

    (@aldiaa)


    This plugin is great and easy to use in single posts, but it is very slow in archive pages (tags, categories, search results, archive by date, etc.). I was trying to optimize it by creating a custom query that gets a list of posts and their views in one shot. However, dealing with transient data in Options table is quite difficult.

    Currently, the best solution I can think of is to get the list of post IDs to be displayed, then run a custom query similar to the following:

    SELECT * FROM _DDV_options where option_value Like '%"post":{"ID":[first_post_id]%' or Like '%"post":{"ID":[second_post_id]%' … or Like '%"post":{"ID":[last_post_id]%';

    After that, parse the JSON values in option_value field to get the views value which looks like (“views”:1234), and finally loop again to display these values in their proper locations in HTML (e.g. next to meta fields).

    I do not think this is the easiest nor the most efficient way to do it. Any suggestions?

    Note: I posted further details in the below thread in www.remarpro.com Forums.

    How to optimize Jetpack Page Visits Counter? | www.remarpro.com

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter aldiaa

    (@aldiaa)

    Update: to avoid parsing JSON in PHP, the custom query should be something like this:

    SELECT
        SUBSTRING_INDEX(
            SUBSTRING_INDEX(
                option_value,
                '"post":{"ID":',
                -1
            ),
            ',"post_author"',
            1
        ) AS Post_ID,
        SUBSTRING_INDEX(
            SUBSTRING_INDEX(option_value, '"views":', -1),
            ',"years":',
            1
        ) AS Views
    FROM
        _DDV_options
    WHERE
        option_value LIKE '%"post":{"ID":1234%'
        or option_value LIKE '%"post":{"ID":5678%'
    
    ...
        or option_value LIKE '%"post":{"ID":9999%';
    Thread Starter aldiaa

    (@aldiaa)

    Update:

    I ran a test for 20 actual posts in my websites and found out that my custom query would not resolve the issue as it is very slow (took about 7.5 seconds).

    It is even worse using regular expressions (took 18.5 seconds).

    The final result would be a list of key-value pairs.

    Is there any alternative? Can’t we save the views counter in postmeta table or as a custom field in posts table?

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic ??

    That’s not an easy problem to solve, unfortunately.

    The data you’re interested in (number of post views for a specific post ID) is originally stored on WordPress.com. When you use a shortcode from my plugin, my plugin relies on the API utilities available in the Jetpack plugin to call the WordPress.com REST API and get the number of views for that post.

    The Jetpack API utility stores that data in a transient that remains valid for 5 minutes. It cannot really afford to cache that data for much longer, since the number of views for a given post is something your site visitors (and you as site admin) would expect to increment as you visit pages.

    You’re seeing that cached data in the screenshot you posted here.

    On my end, in my plugin I’m also doing a bit of caching by saving that data in the postmeta table, just like you suggested. For each post, you’ll find a _post_views key with serialized information about that post’s stats (the total post views for the post, and when it was cached).

    However, for the same reasons as the Jetpack plugin, I cannot really afford to cache data for too long. I cache it for an hour.

    I’m afraid I do not have a perfect solution to this problem. I can, however, make it a bit easier for you to rely on cached data for a longer period.

    I’ve just released a new version of my plugin, and that version introduces a new filter that you will be able to use to cache the data longer. To use that filter, you would add the following snippet to your theme’s functions.php file, or to a functionality plugin:

    /**
     * Cache post views for 6 hours.
     */
    add_filter(
    	'jp_post_views_cache_duration',
    	function () {
    		return 6 * HOUR_IN_SECONDS;
    	}
    );

    In the example below, I’ve extended the default cache period from the 1 hour default to 6 hours instead.

    Let me know if the new version of the plugin, and that filter, help.

    Thread Starter aldiaa

    (@aldiaa)

    Thank you for your clarification @jeherve, I did not notice that there is a _post_views key in postmeta table. Maybe I have searched by a post ID which its cache data had expired. I think this will solve my issue, especially after extending the cache period as per your advice. Thank you for your swift support.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Displaying Post Views in Archive Pages’ is closed to new replies.