Support » Plugin: Post Views Counter » Display all views

  • Resolved bemay1

    (@bemay1)


    Hi, how to display the amount of all views combined on the front end?
    This is a very important feature, thank you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • I do not know if its possible with the plugin itself. But i made this code to do it:
    Place it in your themes functions.php file and then use the shortcode [postviewcounter_total_counts] where you want to display it.

    It refreshes the count every 24 hours (so it wont take too many resources from the server)

    I have made a test page on one of my sites with this code on: https://folkemoedeovernatning.dk/test/ (If you get a 404, i took down the page)

    add_shortcode( 'postviewcounter_total_counts', 'postviewcounter_total_counts_function' );
    function postviewcounter_total_counts_function( $atts ) {
    
        global $wpdb;
    
        // Get stored value (cache)
        $count = get_transient( 'postviewcounter_total_counts' ); // Site Transient
    
        // If no transient (cache) available, then count again.
        if ( false === $count ) {
    
          // Query
          $query = "SELECT sum(count) AS count FROM " . $wpdb->prefix . "post_views";
          $results = $wpdb->get_results( $query );
    
          set_transient( 'postviewcounter_total_counts', $results["0"]->count, DAY_IN_SECONDS );
    
        }
    
      // Return the count
      return $count;
    
    }
    • This reply was modified 8 months, 1 week ago by Kim Vinberg.
    Thread Starter bemay1

    (@bemay1)

    Thank you, thank you, thank you πŸ™‚
    Just to make it perfect, on the generated number how to separate the number to make it easy to read?
    Example: 12,970; 18,099,992;
    Thank you πŸ™‚

    Last line: return number_format($count, 0, ‘.’, ‘.’);

    This should do the trick.

    • This reply was modified 8 months, 1 week ago by Kim Vinberg.
    Thread Starter bemay1

    (@bemay1)

    Well, unfortunately it did not work, can you do a rapid check?
    And how can I thank your dedication?

    Updated mt code on : https://folkemoedeovernatning.dk/test/ It works here. But it could be because f transients you do not have the updated version on your site. Try changing the transient name to something else, like this.

    add_shortcode( 'postviewcounter_total_counts', 'postviewcounter_total_counts_function' );
    function postviewcounter_total_counts_function( $atts ) {
    
        global $wpdb;
    
        // Get stored value (cache)
        $count = get_transient( 'postviewcounterTotalCounts' ); // Site Transient
    
        // If no transient (cache) available, then count again.
        if ( false === $count ) {
    
          // Query
          $query = "SELECT sum(count) AS count FROM " . $wpdb->prefix . "post_views";
          $results = $wpdb->get_results( $query );
    
          set_transient( 'postviewcounterTotalCounts', $results["0"]->count, DAY_IN_SECONDS );
    
        }
    
      // Return the count
      return number_format($count, 0, '.', '.');
    
    }
    • This reply was modified 8 months, 1 week ago by Kim Vinberg.
    Thread Starter bemay1

    (@bemay1)

    It works, thank you so much πŸ™‚

    • This reply was modified 8 months, 1 week ago by bemay1.

    Good to hear πŸ™‚

    Hi, how do we set the views to forever? I do not want the views to reset as I want to show the total number of views for a news post from the time the published and the view count should only increase. It should not reset after any time. Get what I mean?

    Pls let me know. Thanks

    Plugin Author dFactory

    (@dfactory)

    Hi,
    Total are stored forever. What you can remove, especially if you have lot’s of traffic and limited server resources is the daily (detailed) views – using the Reset Data Interval option.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.