Created
March 21, 2018 02:21
-
-
Save nandomoreirame/97dbb6c81fc49ba0e56a0abb60d74558 to your computer and use it in GitHub Desktop.
WordPress Post Views with post_meta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| define('POST_VIEWS_KEY', 'post_views_count'); | |
| function addMetaPostViews( $_postID, $_count = 0 ) { | |
| delete_post_meta( $_postID, POST_VIEWS_KEY ); | |
| add_post_meta( $_postID, POST_VIEWS_KEY, $_count ); | |
| } | |
| function hasViews( $_postID ) { | |
| return !empty( get_post_meta( $_postID, POST_VIEWS_KEY, true ) ) ; | |
| } | |
| // function to display number of posts. | |
| function getPostViews( $_postID ) { | |
| if ( is_object( $_postID ) ) | |
| $_postID = $_postID['id']; | |
| if ( hasViews( $_postID ) ) { | |
| return get_post_meta( $_postID, POST_VIEWS_KEY, true ) . ' visualizações'; | |
| } | |
| addMetaPostViews( $_postID, 0 ); | |
| return "Nenhuma visualização"; | |
| } | |
| // function to count views. | |
| function setPostViews( $_postID ) { | |
| if ( is_object( $_postID ) ) | |
| $_postID = $_postID['id']; | |
| if ( hasViews( $_postID ) ) { | |
| $_totalViews = str_replace( ".", "", get_post_meta( $_postID, POST_VIEWS_KEY, true )); | |
| return update_post_meta( $_postID, POST_VIEWS_KEY, number_format( ((int)$_totalViews + 1), 0, ",", "." ) ); | |
| } | |
| return addMetaPostViews( $_postID, 0 ); | |
| } | |
| function formatViewsNumber( $_number ) { | |
| return number_format( str_replace( ".", "", $_number), 0, ",", "." ); | |
| } | |
| function updatePostViews( $_postID, $_totalViews = 0 ) { | |
| if ( hasViews( $_postID ) ) { | |
| update_post_meta( $_postID, POST_VIEWS_KEY, formatViewsNumber( $_totalViews ) ); | |
| } else { | |
| addMetaPostViews( $_postID, formatViewsNumber( $_totalViews ) ); | |
| } | |
| } | |
| function updateAllPostsViews( $_postType = 'blog' ) { | |
| $wp_query = new WP_Query( | |
| array( | |
| 'post_type' => $_postType, | |
| 'posts_per_page' => -1 | |
| ) | |
| ); | |
| if ($wp_query->have_posts()) { | |
| while($wp_query->have_posts()) { | |
| $wp_query->the_post(); | |
| $_postID = get_the_ID(); | |
| updatePostViews( $_postID, wpp_get_views( $_postID, false ) ); | |
| } | |
| } | |
| } | |
| 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'] = __('Visualizações'); | |
| return $defaults; | |
| } | |
| function posts_custom_column_views( $column_name, $id ) { | |
| if ( $column_name === 'post_views' ) { | |
| // updateViews=1 | |
| if ( isset($_GET['updateViews']) ) { | |
| updatePostViews( get_the_ID(), wpp_get_views( get_the_ID(), false) ); | |
| } | |
| echo getPostViews( get_the_ID() ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment