How to count & display post views without Plugin – in wordpress

Count post views in wordpress

The code below will help you count article views without using the plugin. Just need a few simple code . Note the code below supports you to use the cache comfortably. No need to worry about not counting article views when using caches

Add to functions.php file

The following function helps you get views and  set views of a post. Which uses ajax to count views when using cache. Insert code into the functions.php file

[php]
function getPostViews($postID, $is_single = true){
global $post;
if(!$postID) $postID = $post->ID;
$count_key = ‘post_views_count’;
$count = get_post_meta($postID, $count_key, true);
if(!$is_single){
return ‘<span class="svl_show_count_only">’.$count.’ Views</span>’;
}
$nonce = wp_create_nonce(‘devvn_count_post’);
if($count == "0"){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, ‘0’);
return ‘<span class="svl_post_view_count" data-id="’.$postID.’" data-nonce="’.$nonce.’">0 View</span>’;
}
return ‘<span class="svl_post_view_count" data-id="’.$postID.’" data-nonce="’.$nonce.’">’.$count.’ Views</span>’;
}

function setPostViews($postID) {
$count_key = ‘post_views_count’;
$count = get_post_meta($postID, $count_key, true);
if($count == "0" || empty($count) || !isset($count)){
add_post_meta($postID, $count_key, 1);
update_post_meta($postID, $count_key, 1);
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}

add_action( ‘wp_ajax_svl-ajax-counter’, ‘svl_ajax_callback’ );
add_action( ‘wp_ajax_nopriv_svl-ajax-counter’, ‘svl_ajax_callback’ );
function svl_ajax_callback() {
if ( !wp_verify_nonce( $_REQUEST[‘nonce’], "devvn_count_post")) {
exit();
}
$count = 0;
if ( isset( $_GET[‘p’] ) ) {
global $post;
$postID = intval($_GET[‘p’]);
$post = get_post( $postID );
if($post && !empty($post) && !is_wp_error($post)){
setPostViews($post->ID);
$count_key = ‘post_views_count’;
$count = get_post_meta($postID, $count_key, true);
}
}
die($count.’ Views’);
}

add_action( ‘wp_footer’, ‘svl_ajax_script’, PHP_INT_MAX );
function svl_ajax_script() {
if(!is_single()) return;
?>
<script>
(function($){
$(document).ready( function() {
$(‘.svl_post_view_count’).each( function( i ) {
var $id = $(this).data(‘id’);
var $nonce = $(this).data(‘nonce’);
var t = this;
$.get(‘<?php echo admin_url( ‘admin-ajax.php’ ); ?>?action=svl-ajax-counter&nonce=’+$nonce+’&p=’+$id, function( html ) {
$(t).html( html );
});
});
});
})(jQuery);
</script>
<?php
}
[/php]

Using

Insert the following code into the while loop () where you want to display view views or anywhere with a specific article ID

[php]

echo getPostViews(get_the_ID());
echo getPostViews(12);
echo getPostViews(get_the_ID(), false);

[/php]

Add display post views in admin

[php]
add_filter(‘manage_posts_columns’, ‘posts_column_views’);
add_action(‘manage_posts_custom_column’, ‘posts_custom_column_views’,5,2);
function posts_column_views($defaults){
$defaults[‘post_views’] = __( ‘Views’ , ” );
return $defaults;
}
function posts_custom_column_views($column_name, $id){
if( $column_name === ‘post_views’ ) {
echo getPostViews( get_the_ID(), false);
}
}
[/php]

Query to display posts with the most views.

[php]
$q = new WP_Query(array(
‘post_type’ => ‘post’,
‘posts_per_page’ => 5,
‘meta_key’ => ‘post_views_count’,
‘orderby’ => ‘meta_value_num’,
‘order’ => ‘DESC’
));
if($q->have_posts()):
echo ‘

<ul>’;
while ($q->have_posts()):$q->the_post();
echo ‘

<li>’ . get_the_title() . ‘ – ‘ . getPostViews( get_the_ID(), false) . ‘</li>

‘;
endwhile;
echo ‘</ul>

‘;
endif; wp_reset_query();
[/php]

 

We will be happy to hear your thoughts

Leave a reply

Unix
Logo