how to count the hit of post

Count the post hit and save it to the database

  • Create a database table with fields postid, numberofcounts…


onload of the post page , get its id and store it in the datatable with count

On every load the count number will be increased


Let us consider the table name is tb_hit_count with field postid , and numbercount

global $wpdb;
   $table = $wpdb->prefix.'tb_hit_count';
	$post_id = $wpdb->get_results("SELECT ncount,post_id FROM ".$table." WHERE post_id = ". $id);	if(!empty($post_id))
		$newcount = $post_id[0]->ncount +1;
		//echo $newcount;
		$where = array('post_id'=>$id);
		$data= array('ncount'=>$newcount);
		//echo $cat_parent;
	$wpdb->update( $table, $data, $where);
$datab = array('post_id' => $id, 'parent_category' => 'golf-videos', 'ncount' => 1, );

if the post_id is already in the table , we just update the count , else we insert the post_it values in table.

Leave a Reply

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

1 × 3 =