Advertisement
Guest User

Untitled

a guest
Aug 31st, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.69 KB | None | 0 0
  1. if( !function_exists( 'mars_get_total_views' ) ){
  2.    
  3.     /**
  4.      * Get total view count
  5.      * @param string $post_type (video or post)
  6.      * @return int
  7.      */
  8.    
  9.     function mars_get_total_views( $post_type = 'video' ) {
  10.        
  11.         global $wpdb;
  12.  
  13.         $query = $wpdb->get_var( $wpdb->prepare(
  14.                 "
  15.             SELECT sum(meta_value)
  16.             FROM $wpdb->postmeta LEFT JOIN $wpdb->posts ON ( $wpdb->postmeta.post_id = $wpdb->posts.ID )
  17.             WHERE meta_key = %s
  18.             AND $wpdb->posts.post_status = %s
  19.             AND $wpdb->posts.post_type = %s
  20.             ",
  21.             'count_viewed',
  22.             'publish',
  23.             $post_type
  24.         ) );
  25.        
  26.         return (int)$query > 0 ? $query : 0;
  27.     }
  28. }
  29.  
  30. echo mars_get_total_views( 'video' );
  31.  
  32. echo mars_get_total_views( 'post' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement