Created
October 8, 2012 13:52
-
-
Save Kevinlearynet/3852648 to your computer and use it in GitHub Desktop.
Popular Posts Tracking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Popular posts display | |
* | |
* Display a list of popular posts without the containing element (just LI's) | |
* | |
* @param $size The number of posts to display | |
*/ | |
function base_display_popular_posts( $size = 8 ) { | |
// Query arguments | |
$popular_args = array( | |
'posts_per_page' => $size, | |
'meta_key' => '_base_popular_posts_count', | |
'orderby' => 'meta_value_num' | |
); | |
// The query | |
$popular_posts = new WP_Query( $popular_args ); | |
// The loop | |
while ( $popular_posts->have_posts() ) { | |
$popular_posts->the_post(); | |
echo '<li><a href="' . get_permalink( get_the_ID() ) . '">' . get_the_title() . '</a></li>'; | |
} | |
// Reset post data | |
wp_reset_postdata(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Popular posts tracking | |
* | |
* Tracks the number of logged out user views for a post in a custom field | |
*/ | |
function base_track_popular_posts() { | |
// Only run the process for single posts, pages and post types when the user is logged out | |
if ( is_singular() && !is_user_logged_in() ) { | |
global $post; | |
$custom_field = '_base_popular_posts_count'; | |
// Set/check session | |
if ( !session_id() ) | |
session_start(); | |
// Only track a one view per post for a single visitor session to avoid duplications | |
if ( !isset( $_SESSION["popular-posts-count-{$post->ID}"] ) ) { | |
// Update view count | |
$view_count = get_post_meta( $post->ID, $custom_field, true ); | |
$stored_count = ( isset($view_count) && !empty($view_count) ) ? ( intval($view_count) + 1 ) : 1; | |
$update_meta = update_post_meta( $post->ID, $custom_field, $stored_count ); | |
// Check for errors | |
if ( is_wp_error($update_meta) ) | |
error_log( $update_meta->get_error_message(), 0 ); | |
// Store session in "viewed" state | |
$_SESSION["popular-posts-count-{$post->ID}"] = 1; | |
} | |
// Show view the count for testing purposes (add "?show_count=1" onto the URL) | |
if ( isset($_GET['show_count']) && intval($_GET['show_count']) == 1 ) { | |
echo '<p style="color:red; text-align:center; margin:1em 0;">'; | |
echo get_post_meta( $post->ID, $custom_field, true ); | |
echo ' views of this post</p>'; | |
} | |
} | |
} | |
add_action('wp_head', 'base_track_popular_posts'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Query arguments | |
$today = getdate(); | |
$popular_args = array( | |
'posts_per_page' => $size, | |
'meta_key' => '_base_popular_posts_count', | |
'orderby' => 'meta_value_num' | |
'year' => $today['year'], | |
'monthnum' => $today['mon'], | |
'day' => $today['mday'] | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Query arguments | |
$popular_args = array( | |
'posts_per_page' => $size, | |
'meta_key' => '_base_popular_posts_count', | |
'orderby' => 'meta_value_num' | |
'year' => date('Y'), // Current year | |
'monthnum' => date('n') // Current month | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Query arguments | |
$popular_args = array( | |
'posts_per_page' => $size, | |
'meta_key' => '_base_popular_posts_count', | |
'orderby' => 'meta_value_num' | |
'year' => date('Y'), // Current year | |
'w' => date('W') // Current week | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the gists!