Skip to content

Instantly share code, notes, and snippets.

@malek1316
Last active September 11, 2018 10:22
Show Gist options
  • Save malek1316/af80aebdaa91cdfe3110c016831021db to your computer and use it in GitHub Desktop.
Save malek1316/af80aebdaa91cdfe3110c016831021db to your computer and use it in GitHub Desktop.
How create wp post view count without plugin
//Set the Post Custom Field in the WP dashboard as Name/Value pair Put it functions.php file
function lifestyleblog_PostViews($post_ID) {
//Set the name of the Posts Custom Field.
$count_key = 'post_views_count';
//Returns values of the custom field with the specified key from the specified post.
$count = get_post_meta($post_ID, $count_key, true);
//If the the Post Custom Field value is empty.
if($count == ''){
$count = 0; // set the counter to zero.
//Delete all custom fields with the specified key from the specified post.
delete_post_meta($post_ID, $count_key);
//Add a custom (meta) field (Name/value)to the specified post.
add_post_meta($post_ID, $count_key, '0');
return $count;
//If the the Post Custom Field value is NOT empty.
} else {
$count++; //increment the counter by 1.
//Update the value of an existing meta key (custom field) for the specified post.
update_post_meta($post_ID, $count_key, $count);
//If statement, is just to have the singular form 'View' for the value '1'
if($count == '1'){
return $count;
}
//In all other cases return (count) Views
else {
return $count;
}
}
}
//Put it in single page loop
if(function_exists('lifestyleblog_PostViews')) { lifestyleblog_PostViews(get_the_ID()); }
//Put it in where you want to display the view count
if (function_exists('lifestyleblog_get_PostViews')) {
echo lifestyleblog_get_PostViews(get_the_ID());
}
/*------------------------------------------------------------------------------------------------------------------*/
//Create view column in deshboard post view screen
//Gets the number of Post Views to be used later.
function lifestyleblog_get_PostViews($post_ID){
$count_key = 'post_views_count';
//Returns values of the custom field with the specified key from the specified post.
$count = get_post_meta($post_ID, $count_key, true);
return $count;
}
//Function that Adds a 'Views' Column to your Posts tab in WordPress Dashboard.
function post_column_views($newcolumn){
//Retrieves the translated string, if translation exists, and assign it to the 'default' array.
$newcolumn['post_views'] = __('Views');
return $newcolumn;
}
//Hooks a function to a specific filter action.
//applied to the list of columns to print on the manage posts screen.
add_filter('manage_posts_columns', 'post_column_views');
//Function that Populates the 'Views' Column with the number of views count.
function post_custom_column_views($column_name, $id){
if($column_name === 'post_views'){
// Display the Post View Count of the current post.
// get_the_ID() - Returns the numeric ID of the current post.
echo lifestyleblog_get_PostViews(get_the_ID());
}
}
//Hooks a function to a specific action.
//allows you to add custom columns to the list post/custom post type pages.
//'10' default: specify the function's priority.
//and '2' is the number of the functions' arguments.
add_action('manage_posts_custom_column', 'post_custom_column_views',10,2);
/*====================================================================================================================*/
//Function: Register the 'Views' column as sortable in the WP dashboard. This is for sort by order
function lifestyleblog_register_post_column_views_sortable( $newcolumn ) {
$newcolumn['post_views'] = 'post_views';
return $newcolumn;
}
add_filter( 'manage_edit-post_sortable_columns', 'lifestyleblog_register_post_column_views_sortable' );
//Function: Sort Post Views in WP dashboard based on the Number of Views (ASC or DESC).
function sort_views_column( $vars )
{
if ( isset( $vars['orderby'] ) && 'post_views' == $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => 'post_views_count', //Custom field key
'orderby' => 'meta_value_num') //Custom field value (number)
);
}
return $vars;
}
add_filter( 'request', 'sort_views_column' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment