znalazłem fajny skrypt, który wyświetla posty, które były najcześciej przglądane. Jednak chciałbym, żeby statystyki resetowały się co tydzień i w każdym tygodniu liczenie zaczynało się od zera i nie potrafie za bardzo tego skumać:
funtions.php
function wpb_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); function wpb_track_post_views ($post_id) { if ( !is_single() ) return; $post_id = $post->ID; } wpb_set_post_views($post_id); } add_action( 'wp_head', 'wpb_track_post_views'); function wpb_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'; }
Wyświetlanie:
<?php $popularpost = new WP_Query( array( 'posts_per_page' => 5, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC' ) ); while ( $popularpost->have_posts() ) : $popularpost->the_post();?> <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <p><?php the_excerpt(); ?></p> <a href="<?php the_permalink() ?>"><img src="<?php bloginfo( 'template_url' ); ?>/images/ogladaj.png"></a> <img class="line" src="<?php bloginfo( 'template_url' ); ?>/images/line.png"> <?php endwhile; ?>