In WordPress, the term “post view count” refers to the number of times a specific post or page has been viewed by visitors on your website. It is a metric used to measure the popularity or engagement of a particular piece of content.
Post view count is typically tracked and displayed using plugins or tracking tools specifically designed for WordPress. These plugins integrate with your website and keep a record of the number of times a post or page has been accessed.
To get the post view count in WordPress, you can use a combination of WordPress functions and plugins. Here are two methods you can try:
Method 1: Using a Plugin (such as “Post Views Counter”):
- Install and activate the “Post Views Counter” plugin from the WordPress Plugin Directory.
- Once activated, the plugin will automatically start tracking the view count for your posts.
- To display the view count on your posts, you can use a shortcode provided by the plugin. Edit your post or page and add the shortcode
[post-views]
to the desired location. This will display the view count when the post is viewed.
Method 2: Manual Implementation:
- Open your theme’s
functions.php
file for editing. You can find this file in your WordPress theme’s directory. - Add the following one of functions code below to the
functions.php
file:
function get_post_view_count($post_id) {
$count_key = 'post_views_count';
$count = get_post_meta($post_id, $count_key, true);
if ($count == '') {
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
return "0";
}
return $count;
}
function set_post_view_count($post_id) {
$count_key = 'post_views_count';
$count = get_post_meta($post_id, $count_key, true);
if ($count == '') {
$count = 0;
delete_post_meta($post_id, $count_key);
add_post_meta($post_id, $count_key, '0');
} else {
$count++;
update_post_meta($post_id, $count_key, $count);
}
}
// Add the following code where you want to display the view count, e.g., single.php, content.php, etc.
$post_id = get_the_ID();
set_post_view_count($post_id);
$view_count = get_post_view_count($post_id);
echo 'Post Views: ' . $view_count;
- Save the
functions.php
file and upload it back to your server. - The code will update the view count whenever a post is viewed, and you can display the view count using the provided
echo
statement.
Note: If you’re not comfortable editing theme files directly, you can use a child theme or a custom plugin to add the code instead.
Remember to backup your theme files or create a child theme before making any changes to avoid losing your modifications in case of future theme updates.