Created
October 8, 2013 22:57
-
-
Save Roberto-Sudo/6893243 to your computer and use it in GitHub Desktop.
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
To call the function I do something like this: | |
$cat = '2,6,3,4,12,11,17,16,125,1,13,9,7,15,49,51'; | |
$posts = get_popular_posts_array( array( 'range' => 'all', 'limit' => 24, 'cat' => $cat, 'pid' => '2694,2696,2699' ) ); | |
And then I do a foreach using the $posts array. This way I can even use it twice or as many times as I want without repeating the query. |
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 | |
// This function requires Wordpress Popular Posts plugin to be active. | |
// http://wordpress.org/plugins/wordpress-popular-posts/ | |
function get_popular_posts_array($instance, $return = false) { | |
if( !class_exists( 'WordpressPopularPosts' ) ) return array(); | |
$wpp = new WordpressPopularPosts(); | |
// update instance's settings | |
// echo "<pre>"; print_r($instance); echo "</pre>"; | |
$instance = $wpp->array_merge_recursive_distinct($wpp->defaults, $instance); | |
// echo "<pre>"; print_r($instance); echo "</pre>"; | |
global $wpdb; | |
$table = $wpdb->prefix . "popularpostsdata"; | |
$fields = ""; | |
$from = ""; | |
$where = ""; | |
$post_types = ""; | |
$pids = ""; | |
$cats = ""; | |
$authors = ""; | |
$content = ""; | |
// post filters | |
// * post types - based on code seen at https://github.com/williamsba/WordPress-Popular-Posts-with-Custom-Post-Type-Support | |
$types = explode(",", $instance['post_type']); | |
$i = 0; | |
$len = count($types); | |
$sql_post_types = ""; | |
$join_cats = true; | |
if ($len > 1) { // we are getting posts from more that one ctp | |
foreach ( $types as $post_type ) { | |
$sql_post_types .= "'" .$post_type. "'"; | |
if ($i != $len - 1) $sql_post_types .= ","; | |
$i++; | |
} | |
$post_types = " p.post_type IN({$sql_post_types}) "; | |
} else if ($len == 1) { // post from one ctp only | |
$post_types = " p.post_type = '".$instance['post_type']."' "; | |
// if we're getting just pages, why join the categories table? | |
if ( strtolower($instance['post_type']) == 'page' ) | |
$join_cats = false; | |
} | |
// * posts exclusion | |
if ( !empty($instance['pid']) ) { | |
$ath = explode(",", $instance['pid']); | |
$len = count($ath); | |
if ($len > 1) { // we are excluding more than one post | |
$pids = " AND p.ID NOT IN(".$instance['pid'].") "; | |
} else if ($len == 1) { // exclude one post only | |
$pids = " AND p.ID <> '".$instance['pid']."' "; | |
} | |
} | |
// * categories | |
if ( !empty($instance['cat']) && $join_cats ) { | |
$cat_ids = explode(",", $instance['cat']); | |
$in = array(); | |
$out = array(); | |
$not_in = ""; | |
usort($cat_ids, array(&$wpp, 'sorter')); | |
for ($i=0; $i < count($cat_ids); $i++) { | |
if ($cat_ids[$i] >= 0) $in[] = $cat_ids[$i]; | |
if ($cat_ids[$i] < 0) $out[] = $cat_ids[$i]; | |
} | |
$in_cats = implode(",", $in); | |
$out_cats = implode(",", $out); | |
$out_cats = preg_replace( '|[^0-9,]|', '', $out_cats ); | |
if ($in_cats != "" && $out_cats == "") { // get posts from from given cats only | |
$cats = " AND p.ID IN ( | |
SELECT object_id | |
FROM $wpdb->term_relationships AS r | |
JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id | |
JOIN $wpdb->terms AS t ON t.term_id = x.term_id | |
WHERE x.taxonomy = 'category' AND t.term_id IN($in_cats) | |
) "; | |
} else if ($in_cats == "" && $out_cats != "") { // exclude posts from given cats only | |
$cats = " AND p.ID NOT IN ( | |
SELECT object_id | |
FROM $wpdb->term_relationships AS r | |
JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id | |
JOIN $wpdb->terms AS t ON t.term_id = x.term_id | |
WHERE x.taxonomy = 'category' AND t.term_id IN($out_cats) | |
) "; | |
} else { // mixed, and possibly a heavy load on the DB | |
$cats = " AND p.ID IN ( | |
SELECT object_id | |
FROM $wpdb->term_relationships AS r | |
JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id | |
JOIN $wpdb->terms AS t ON t.term_id = x.term_id | |
WHERE x.taxonomy = 'category' AND t.term_id IN($in_cats) | |
) AND p.ID NOT IN ( | |
SELECT object_id | |
FROM $wpdb->term_relationships AS r | |
JOIN $wpdb->term_taxonomy AS x ON x.term_taxonomy_id = r.term_taxonomy_id | |
JOIN $wpdb->terms AS t ON t.term_id = x.term_id | |
WHERE x.taxonomy = 'category' AND t.term_id IN($out_cats) | |
) "; | |
} | |
} | |
// * authors | |
if ( !empty($instance['author']) ) { | |
$ath = explode(",", $instance['author']); | |
$len = count($ath); | |
if ($len > 1) { // we are getting posts from more that one author | |
$authors = " AND p.post_author IN(".$instance['author'].") "; | |
} else if ($len == 1) { // post from one author only | |
$authors = " AND p.post_author = '".$instance['author']."' "; | |
} | |
} | |
$fields = "p.ID AS 'id', p.post_title AS 'title', p.post_date AS 'date', p.post_author AS 'uid' "; | |
if ($instance['range'] == "all") { // ALL TIME | |
$fields .= ", p.comment_count AS 'comment_count' "; | |
if ($instance['order_by'] == "comments") { // ordered by comments | |
if ($instance['stats_tag']['views']) { // get views, too | |
$fields .= ", IFNULL(v.pageviews, 0) AS 'pageviews' "; | |
$from = " {$wpdb->posts} p LEFT JOIN {$table} v ON p.ID = v.postid WHERE {$post_types} {$pids} {$authors} {$cats} AND p.comment_count > 0 AND p.post_password = '' AND p.post_status = 'publish' ORDER BY p.comment_count DESC LIMIT {$instance['limit']} "; | |
} else { // get data from wp_posts only | |
$from = " {$wpdb->posts} p WHERE {$post_types} {$pids} {$authors} {$cats} AND p.comment_count > 0 AND p.post_password = '' AND p.post_status = 'publish' ORDER BY p.comment_count DESC LIMIT {$instance['limit']} "; | |
} | |
} else { // ordered by views / avg | |
if ( $instance['order_by'] == "views" ) { | |
$fields .= ", v.pageviews AS 'pageviews' "; | |
$from = " {$table} v LEFT JOIN {$wpdb->posts} p ON v.postid = p.ID WHERE {$post_types} {$pids} {$authors} {$cats} AND p.post_password = '' AND p.post_status = 'publish' ORDER BY pageviews DESC LIMIT {$instance['limit']} "; | |
} else if ( $instance['order_by'] == "avg" ) { | |
$fields .= ", ( v.pageviews/(IF ( DATEDIFF('{$wpp->now()}', MIN(v.day)) > 0, DATEDIFF('{$wpp->now()}', MIN(v.day)), 1) ) ) AS 'avg_views' "; | |
$from = " {$table} v LEFT JOIN {$wpdb->posts} p ON v.postid = p.ID WHERE {$post_types} {$pids} {$authors} {$cats} AND p.post_password = '' AND p.post_status = 'publish' GROUP BY p.ID ORDER BY avg_views DESC LIMIT {$instance['limit']} "; | |
} | |
} | |
} else { // CUSTOM RANGE | |
$interval = ""; | |
switch( $instance['range'] ){ | |
case "yesterday": | |
$interval = "1 DAY"; | |
break; | |
case "daily": | |
$interval = "1 DAY"; | |
break; | |
case "weekly": | |
$interval = "1 WEEK"; | |
break; | |
case "monthly": | |
$interval = "1 MONTH"; | |
break; | |
default: | |
$interval = "1 DAY"; | |
break; | |
} | |
if ($instance['order_by'] == "comments") { // ordered by comments | |
$fields .= ", c.comment_count AS 'comment_count' "; | |
$from = " (SELECT comment_post_ID AS 'id', COUNT(comment_post_ID) AS 'comment_count', MAX(comment_date) AS comment_date FROM {$wpdb->comments} WHERE comment_date > DATE_SUB('{$wpp->now()}', INTERVAL {$interval}) AND comment_approved = 1 GROUP BY id ORDER BY comment_count DESC, comment_date DESC) c LEFT JOIN {$wpdb->posts} p ON p.ID = c.id "; | |
if ($instance['stats_tag']['views']) { // get views, too | |
$fields .= ", IFNULL(v.pageviews, 0) AS 'pageviews' "; | |
$from .= " LEFT JOIN (SELECT id, SUM(pageviews) AS pageviews, MAX(day) AS day FROM {$table}cache WHERE day > DATE_SUB('{$wpp->now()}', INTERVAL {$interval}) GROUP BY id ORDER BY pageviews DESC, day DESC) v ON p.ID = v.id "; | |
} | |
$from .= " WHERE {$post_types} {$pids} {$authors} {$cats} AND p.post_password = '' AND p.post_status = 'publish' LIMIT {$instance['limit']} "; | |
} else { // ordered by views / avg | |
if ( $instance['order_by'] == "views" ) { | |
$fields .= ", v.pageviews AS 'pageviews' "; | |
$from = " (SELECT id, SUM(pageviews) AS pageviews, MAX(day) AS day FROM {$table}cache WHERE day > DATE_SUB('{$wpp->now()}', INTERVAL {$interval}) GROUP BY id ORDER BY pageviews DESC, day DESC) v LEFT JOIN {$wpdb->posts} p ON v.id = p.ID "; | |
} else if ( $instance['order_by'] == "avg" ) { | |
$fields .= ", ( v.pageviews/(IF ( DATEDIFF('{$wpp->now()}', DATE_SUB('{$wpp->now()}', INTERVAL {$interval})) > 0, DATEDIFF('{$wpp->now()}', DATE_SUB('{$wpp->now()}', INTERVAL {$interval})), 1) ) ) AS 'avg_views' "; | |
$from = " (SELECT id, SUM(pageviews) AS pageviews, MAX(day) AS day FROM {$table}cache WHERE day > DATE_SUB('{$wpp->now()}', INTERVAL {$interval}) GROUP BY id ORDER BY pageviews DESC, day DESC) v LEFT JOIN {$wpdb->posts} p ON v.id = p.ID "; | |
} | |
if ( $instance['stats_tag']['comment_count'] ) { // get comments, too | |
$fields .= ", IFNULL(c.comment_count, 0) AS 'comment_count' "; | |
$from .= " LEFT JOIN (SELECT comment_post_ID AS 'id', COUNT(comment_post_ID) AS 'comment_count', MAX(comment_date) AS comment_date FROM {$wpdb->comments} WHERE comment_date > DATE_SUB('{$wpp->now()}', INTERVAL {$interval}) AND comment_approved = 1 GROUP BY id ORDER BY comment_count DESC, comment_date DESC) c ON p.ID = c.id "; | |
} | |
$from .= " WHERE {$post_types} {$pids} {$authors} {$cats} AND p.post_password = '' AND p.post_status = 'publish' "; | |
if ( $instance['order_by'] == "avg" ) { | |
$from .= " GROUP BY v.id ORDER BY avg_views DESC "; | |
} | |
$from .= " LIMIT {$instance['limit']} "; | |
} | |
} | |
$query = "SELECT {$fields} FROM {$from}"; | |
//echo $query; | |
$mostpopular = $wpdb->get_results($query); | |
// Custom WP Post array output. | |
$posts_array = array(); | |
foreach ( $mostpopular as $mp ) { | |
array_push( $posts_array, get_post( $mp->id ) ); | |
} | |
// echo '<pre>' . print_r($posts_array) . '</pre>'; | |
return $posts_array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment