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>
Comment
[php] Your code [/php]