• Resolved RezaY

    (@mohammad983)


    Hi
    I used this tutorial for track post view on WP
    But when I enable Light Speed Cache , it does not work!
    How to fix it?

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Hai Zheng?

    (@hailite)

    This kind of track can’t work with cache. Please try an ajax track way.

    Thread Starter RezaY

    (@mohammad983)

    thanks for your quick replay
    is there Fragment Caching in light speed cache?
    in this tutorial it fix it on W3 total cach

    Plugin Support Hai Zheng?

    (@hailite)

    Yes in LSCWP its called ESI: https://www.litespeedtech.com/support/wiki/doku.php/litespeed_wiki:cache:lscwp:esi_sample

    Please try the following code:

    1. Change setPostViews(get_the_ID()); to

    
    LiteSpeed_Cache_API::hook_tpl_esi('your_count_esi', 'hook_esi' );
    function hook_esi( $param ) {
        setPostViews( $param[ 'id' ] ) ;
        exit;
    }
    
    echo LiteSpeed_Cache_API::esi_url( 'your_count_esi', 'RezaY test', array( 'id' => get_the_ID() ) ) ;
    
    
    Thread Starter RezaY

    (@mohammad983)

    it didnt work.
    it echo a list of may recent post!

    Plugin Support Hai Zheng?

    (@hailite)

    For

    
    LiteSpeed_Cache_API::hook_tpl_esi('your_count_esi', 'hook_esi' );
    function hook_esi( $param ) {
        setPostViews( $param[ 'id' ] ) ;
        exit;
    }
    

    part, please put into functions.php. It needs a higher priority to run, before all theme codes output.

    • This reply was modified 7 years, 1 month ago by Hai Zheng?.
    Thread Starter RezaY

    (@mohammad983)

    I put these in function.php

    function getPostViews($postID)
    {
        $count_key = 'post_views_count';
        $count = get_post_meta($postID, $count_key, true);
        if ($count == '') {
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
            return "0 View";
        }
        return $count . ' Views';
    }
    
    function setPostViews($postID)
    {
        $count_key = 'post_views_count';
        $count = get_post_meta($postID, $count_key, true);
        if ($count == '') {
            $count = 0;
            delete_post_meta($postID, $count_key);
            add_post_meta($postID, $count_key, '0');
        } else {
            $count++;
            update_post_meta($postID, $count_key, $count);
        }
    }
    // Remove issues with prefetching adding extra views
    remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
    
    LiteSpeed_Cache_API::hook_tpl_esi('your_count_esi', 'hook_esi' );
    function hook_esi( $param ) {
        setPostViews( $param[ 'id' ] ) ;
        exit;
    }
    

    and this in single php
    `
    <?php
    echo LiteSpeed_Cache_API::esi_url( ‘your_count_esi’, ‘RezaY test’, array( ‘id’ => get_the_ID() ) ) ;

    ?>
    <?php echo getPostViews(get_the_ID()); ?>
    `

    but after echo lightspeed it shows my homepage with all of my recent post!

    • This reply was modified 7 years, 1 month ago by RezaY.
    Plugin Support LiteSpeed Lisa

    (@lclarke)

    Hi, @mohammad983

    Are you still experiencing a problem with this?

    While we don’t mind making suggestions now and then, it’s outside of the scope of our free support to debug custom code. Have you looked at our API documentation? You should be able to find descriptions of how our API functions work, as well as a few specific ESI examples.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Fix post view in lightspeed cache’ is closed to new replies.