Showing accurate post view counts on archive pages

67

July 31, 2024

August 27, 2024

00:00:00

Today:Saturday

2024/06/05

  • home
  • Wordpress
  • Showing accurate post view counts on archive pages

Estimated reading time: 6 minutes


Font Size

Excerpt from this article:

This function in WordPress is designed to calculate the actual number of views for a post. By filtering out views originating from search engines or direct page visits, it provides a more accurate and useful count of genuine user views. The calculated results are stored in a cookie for subsequent requests.

  • Supporting us, a step towards progress!

    • With your help, we can continue our journey of providing high-quality and valuable content. Your support, no matter how small, gives us the motivation and power to take firmer steps on this challenging path.


      Two simple ways to support us:
      1. Contribute to cryptocurrency mining: Share some of your system's processing power to help us mine cryptocurrencies.
      2.  Send cryptocurrency: You can directly send cryptocurrency to our wallet.


      With your support, a world of useful information will be available to everyone!

To get to know us better, check out our About Us page.

We dedicate countless hours and energy to creating high-quality, informative content for you. Your support is what motivates us to continue this journey.

We sincerely appreciate your support.

1,000,000 Hashes (Total) = 0.000,000,03 BTC

1 BTC BitTorrentBTTC

2 BTC BEP-20PEPE

3 BTC ShibaSHIB

4 BTC WINkLinkWIN

5 BTC RadiocacaRACA

6 BTC FlokiFLOKI

7 BTC EverdomeDOME

8 BTC DentDENT

9 BTC ReefREEF

10 BTC HoloHOT

11 BTC LeverFiLEVER

12 BTC AmpAMP

13 BTC Reserve rightsRSR

14 BTC AnkrANKR

15 BTC Beta FinanceBETA

16 BTC TRONTRX

17 BTC DogecoinDOGE

18 BTC TetherUSDT

19 BTC Chorono.techTIME

20 BTC CompoundCOMP

21 BTC BitcoinCashBCH

22 BTC BinanceCoinBNB

23 BTC EthereumETH

24 BTC BitcoinBTC

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit BTTC only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BTTC only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit PEPE only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit PEPE only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit SHIB only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit SHIB only to this address.

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit WIN only to this address.

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit WIN only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit RACA only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit RACA only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit FLOKI only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit FLOKI only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit DOME only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit DOME only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit DENT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit DENT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit REEF only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit REEF only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit HOT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit HOT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit LEVER only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit LEVER only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit AMP only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit AMP only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit RSR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit RSR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit ANKR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit ANKR only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit BETA only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BETA only to this address.

We sincerely appreciate your support. The provided address is defined on the TRON network.

Please deposit TRX only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit TRX only to this address.

We sincerely appreciate your support. The provided address is defined on the DOGECOIN network.

Please deposit DOGE only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit DOGE only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit USDT only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit USDT only to this address.

We sincerely appreciate your support. The provided address is defined on the TRC-20 network.

Please deposit USDT only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit TIME only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit TIME only to this address.

We sincerely appreciate your support. The provided address is defined on the ERC-20 network.

Please deposit COMP only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit COMP only to this address.

We sincerely appreciate your support. The provided address is defined on the BITCOINCASH network.

Please deposit BCH only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BCH only to this address.

We sincerely appreciate your support. The provided address is defined on the BSC network.

Please deposit BNB only to this address.

We sincerely appreciate your support. The provided address is defined on the BSC network.

Please deposit BNB only to this address.

We sincerely appreciate your support. The provided address is defined on the ETHEREUM network.

Please deposit ETH only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit ETH only to this address.

We sincerely appreciate your support. The provided address is defined on the BITCOIN network.

Please deposit BTC only to this address.

We sincerely appreciate your support. The provided address is defined on the BEP-20 network.

Please deposit BTC only to this address.

Full content of the article:

On category or tag archive pages, there are always cards displayed to showcase posts.

To enhance the professionalism of these cards, WordPress theme designers often add metrics like view count, comment count, or even the post’s publication or modification date.

In this article, we’ll demonstrate how to create a shortcode for use in archive.php, category.php, and tag.php pages to display accurate post view counts on these cards, thereby improving the visual appeal.

It’s crucial to note that this code should filter out fake views (such as page refreshes or views from search engines) to ensure accurate post view counts.** **

Note: The provided code should be placed in the functions.php file.

 

Overall Code Explanation:

get_post_views_filtered function:

Purpose: Calculates the filtered view count for a post.

**

Mechanism:**
● Extracts the total view count of the post from its metadata.
** ● Checks the user’s cookies to determine if the user’s views have already been filtered.
If the views haven’t been filtered, it analyzes the referral source to identify direct page visits and considers them as valid views.
** ● The final result is stored in a cookie to avoid recalculation on subsequent visits.

**

**

set_filtered_views_cookie function:

Purpose: Stores the filtered view count in a cookie.

**

Mechanism:**
● This function is called within the wp_head action.
** ● The setcookie function is used to store the filtered view count in a cookie with a 24-hour expiration.

 

Technical Explanations:

get_post_meta: Retrieves the value of a specific meta data for a post.
** ● wp_get_referer: Returns the URL of the page that referred the user to the current page.
** ● parse_url: Parses a URL and separates its components such as protocol, host, and path.
** ● setcookie: Sets a cookie in the user’s browser.

 

// Copy to functions.php
function get_post_views_filtered($post_id) {
    $count_key = 'post_views_count';
    $post_views = (int) get_post_meta($post_id, $count_key, true);
    // If the views have already been filtered, return them
    if (isset($_COOKIE['post_views_filtered_' . $post_id])) {
      $filtered_views = (int) $_COOKIE['post_views_filtered_' . $post_id];
      return $filtered_views;
    }
    // If the views are not already filtered, filter and save them
    $filtered_views = 0;
    if ($post_views > 0) {
      // Extract hits that came from a search or a direct visit to the post page
      $referers = wp_get_referer();
      if (empty($referers) || parse_url($referers, PHP_URL_HOST) === parse_url(get_permalink($post_id), PHP_URL_HOST)) {
        $filtered_views = $post_views;
      }
    }
  // Pass the cookie setting to the function
  add_action('wp_head', 'set_filtered_views_cookie', 10);
    return $filtered_views;
  }
  function set_filtered_views_cookie() {
    global $post_id, $filtered_views;
    if (!empty($post_id) && !empty($filtered_views)) {
      setcookie('post_views_filtered_' . $post_id, $filtered_views, time() + 60 * 60 * 24, '/');
    }
  }

 

Noteworthy points:

● You should place the above code in the functions.php file.
** ● This code utilizes a cookie to store information, which may have certain limitations in some cases.
** ● To enhance the accuracy of calculations, more complex methods can be employed to identify duplicate visits.
** ● This code serves as a mere example of calculating filtered views in WordPress and may require modifications for different projects.

 

Calling the Shortcode:

Thus far, we’ve incorporated the complete code for displaying a post’s view count into the functions.php file; however, it won’t produce any output until it’s called from the appropriate location.

To invoke the written code, we’ll use the following shortcode within HTML tags. By applying some styling, we can visually enhance its presentation within the post card.

<div class="SC_box-displayVisitCard">
<?php
$post_id = get_the_ID(); // Get the current post ID outside the loop
$filtered_views = get_post_views_filtered($post_id); // Get filtered views for the current post
?>
<p><i class="fas fa-user-check"></i><?php echo number_format($filtered_views); ?></p> 
</div>

You can download the complete and tested code from the following link:

Download

Archive ,  Articles ,  Category ,  PHP ,  Tag ,  Wordpress .

Comment

[php] Your code [/php]

Leave a Reply

Your email address will not be published.Required fields are marked *

  • All Our Articles

    • To provide you with an overview of all our published articles, we have created this section. In our articles, we will focus on the practical application of the material, rather than just filling the site with content. Therefore, our articles will be few but very practical. Our articles will cover a wide range of technology and programming topics.

UTC Time Zone
Read More
Fetching database visit statistics using PHP without plugins
Read More
Examination of title code within WordPress templates
Read More
Adding a Period to Categories in WordPress: A Simple and Effective Solution
Read More
Retrieving and Displaying a Complete List of WordPress Site Categories
Read More
Displaying Category Name and Post Count on Category Archive Page
Read More
Showing accurate post view counts on archive pages
Read More

Fonts made from Web Fonts is licensed by CC BY 4.0

13,155