PHP Code to Display Post Views Without A Plugin In WordPress

Spread the love


If you feel comfortable editing code, you can display your post views with a few changes to your theme files. Follow the instructions below.

  • Log in to your WordPress dashboard.
  • Go to Appearance > Theme Editor.
  • Open the functions.php file.
  • Copy and paste the following code before the closing tag ?>:

function gt_get_post_view() {

$count = get_post_meta( get_the_ID(), ‘post_views_count’, true );

return “$count views”;


function gt_set_post_view() {

$key = ‘post_views_count’;

$post_id = get_the_ID();

$count = (int) get_post_meta( $post_id, $key, true );


update_post_meta( $post_id, $key, $count );


function gt_posts_column_views( $columns ) {

$columns[‘post_views’] = ‘Views’;

return $columns;


function gt_posts_custom_column_views( $column ) {

if ( $column === ‘post_views’) {

echo gt_get_post_view();



add_filter( ‘manage_posts_columns‘, ‘gt_posts_column_views‘ );

add_action( ‘manage_posts_custom_column‘, ‘gt_posts_custom_column_views‘ );


  • Open the single.php file. Copy and paste this code in the while loop (press CTRL + F or Command + F to find it in the file):



<?php gt_set_post_view(); ?>


  • Copy and paste this code where you want to show the number of views:



<?= gt_get_post_view(); ?>

Leave a Reply

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