Get popular custom post type post by views
-
Hi everybody!.
I have a problem with try to get popular post by views.//////update post view function wp_set_post_views($postID) { $count_key = 'wpb_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); } } //To keep the count accurate, lets get rid of prefetching remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); //show view function wp_get_post_views($postID){ $count_key = 'wpb_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'; } //////////this is my code to get list by views: $arg = array( "post_type" => "product", "posts_per_page" => 10, "meta_key" => "wpb_post_views_count", "orderby" => "meta_value_num", "order" => "DESC", ); $query = new wp_query($arg); if($query->have_posts()) : while($query->have_posts()) : $query->the_post(); the_title(); echo '('.wp_get_post_views(get_the_ID()).')'; endwhile; endif; ?>
But the result is not correct. The list is still sort by post ID.
where is my error. How can i fix it. Thanks!
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
- The topic ‘Get popular custom post type post by views’ is closed to new replies.