Post views
-
Hi again! The plugin is working really good at the moment but I have a question about the views.
Is there any function to get the post views count? Look, my theme uses this code to show popular posts in a php template:
<?php $pop_days = esc_html(get_option('mvp_pop_days')); $pop_num = esc_html(get_option('mvp_pop_num')); $popular_days_ago = "$pop_days days ago"; $recent = new WP_Query(array('posts_per_page' => $pop_num, 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'post_views_count', 'date_query' => array( array( 'after' => $popular_days_ago )) )); while($recent->have_posts()) : $recent->the_post(); ?>
I think the only thing I need is change: ‘post_views_count’ to ‘-ga:pageviews’ ??? But I know that I need a function for it, because the theme uses this function to get the post views:
if ( !function_exists( 'getCrunchifyPostViews' ) ) { function getCrunchifyPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); return "0 View"; } return $count.' Views'; } } if ( !function_exists( 'setCrunchifyPostViews' ) ) { function setCrunchifyPostViews($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ $count = 0; delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, '0'); }else{ $count++; update_post_meta($postID, $count_key, $count); } } }
It’s a trashy way to count the post views and that’s why I switched to your plugin!
- The topic ‘Post views’ is closed to new replies.