Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if( !function_exists( 'mars_get_total_views' ) ){
- /**
- * Get total view count
- * @param string $post_type (video or post)
- * @return int
- */
- function mars_get_total_views( $post_type = 'video' ) {
- global $wpdb;
- $query = $wpdb->get_var( $wpdb->prepare(
- "
- SELECT sum(meta_value)
- FROM $wpdb->postmeta LEFT JOIN $wpdb->posts ON ( $wpdb->postmeta.post_id = $wpdb->posts.ID )
- WHERE meta_key = %s
- AND $wpdb->posts.post_status = %s
- AND $wpdb->posts.post_type = %s
- ",
- 'count_viewed',
- 'publish',
- $post_type
- ) );
- return (int)$query > 0 ? $query : 0;
- }
- }
- echo mars_get_total_views( 'video' );
- echo mars_get_total_views( 'post' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement