Last active
January 21, 2016 20:31
-
-
Save RadGH/dc45412e8ece6e0d9058 to your computer and use it in GitHub Desktop.
Wordpress Popular Posts - Store views for existing 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
<?php | |
/* | |
Include this script then access your wordpress website with the URL ending in: ?wppscan=1 | |
It will go through each post, 10 at a time, and update the views total/daily/weekly/monthly as post meta. | |
This is to co-exist with your own option that saves these values periodically when saving post. That functionality is not included here. | |
*/ | |
global $ld_recalc; | |
$ld_recalc = array( | |
'posts_per_run' => 10, | |
'post_types' => array( 'post' ), | |
'scan_key' => 'wppscan', | |
'scan_identifier' => '5016-01-20', // Change this if you want to scan again in the future. | |
); | |
function recalc_acf_posts_init() { | |
if ( !isset($_REQUEST['wppscan']) ) return; | |
global $ld_recalc; | |
$args = array( | |
'post_type' => $ld_recalc['post_types'], | |
'meta_query' => array( | |
'relation' => 'OR', | |
array( | |
'key' => $ld_recalc['scan_key'], | |
'compare' => 'NOT EXISTS', | |
'value' => ' ', | |
), | |
array( | |
'key' => $ld_recalc['scan_key'], | |
'compare' => '!=', | |
'value' => $ld_recalc['scan_identifier'], | |
), | |
), | |
'posts_per_page' => $ld_recalc['posts_per_run'], | |
); | |
$posts = new WP_Query($args); | |
if ( $posts->have_posts() ) { | |
echo '<h2>Scanning '. min($posts->found_posts, $ld_recalc['posts_per_run']).' of '. $posts->found_posts.'...</h2>'; | |
echo '<div style="font-size: 14px; font-family: Arial, sans-serif;">'; | |
foreach( $posts->posts as $i => $p ) { | |
echo '<div style="width: 33%; float: left; overflow: hidden; position: relative;">'; | |
$url = get_permalink( $p->ID ); | |
$edit = get_edit_post_link( $p->ID ); | |
echo '<p><strong>'.($i+1).') '.ucwords(str_replace(array('-','_'), ' ', $p->post_type)).' #'. $p->ID .'</strong> - <a href="'.esc_attr($url).'" target="_blank">'. esc_html($p->post_title) .'</a> (<a href="'.esc_attr($edit).'" target="_blank">Edit</a>)</p>'; | |
$total = wpp_get_views( $postid ); | |
$daily = wpp_get_views( $postid, 'daily' ); | |
$weekly = wpp_get_views( $postid, 'weekly' ); | |
$monthly = wpp_get_views( $postid, 'monthly' ); | |
update_post_meta( $postid, 'views_total', $total ); | |
update_post_meta( $postid, 'views_daily', $daily ); | |
update_post_meta( $postid, 'views_weekly', $weekly ); | |
update_post_meta( $postid, 'views_monthly', $monthly ); | |
echo '<pre style="border-bottom: 1px solid #666; padding-bottom: 15px; margin: 15px 0;">'; | |
echo "Total Views: ", print_r($total, true), "\n"; | |
echo "Daily: ", print_r($daily, true), "\n"; | |
echo "Weekly: ", print_r($weekly, true), "\n"; | |
echo "Monthly: ", print_r($monthly, true), "\n"; | |
echo '</pre>'; | |
echo '</div>'; | |
update_post_meta( $p->ID, $ld_recalc['scan_key'], $ld_recalc['scan_identifier'] ); | |
} | |
echo '</div>'; | |
}else{ | |
echo '<p><strong>Scan Completed:</strong> All posts have been updated!</p>'; | |
} | |
exit; | |
} | |
add_action( 'wp', 'recalc_acf_posts_init' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment