Last active
November 9, 2015 23:13
-
-
Save DylanVann/20a4631cae434a72e93d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| function wpp_get_posts($args) { | |
| global $wpdb; | |
| $prefix = $wpdb->prefix; | |
| $wpp_prefix = $wpdb->prefix . "popularposts"; | |
| $select = "SELECT SQL_CALC_FOUND_ROWS"; | |
| $from = " FROM"; | |
| $where = " WHERE 1=1"; | |
| $groupby = " GROUP BY"; | |
| $having = ""; | |
| $orderby = " ORDER BY"; | |
| $now = date('Y-m-d'); | |
| $select .= " p.ID AS 'id'"; | |
| if ($args['orderby'] == 'popular') { | |
| $from .= " {$wpp_prefix}data v"; | |
| $from .= " LEFT JOIN {$prefix}posts p ON v.postid = p.ID"; | |
| $from .= " LEFT JOIN {$prefix}term_relationships tr ON tr.object_id = v.postid"; | |
| $from .= " LEFT JOIN {$prefix}term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id"; | |
| $from .= " LEFT JOIN {$prefix}terms t ON t.term_id = tt.term_taxonomy_id"; | |
| $orderby .= " v.pageviews DESC"; | |
| if ($args['popular_range']) { | |
| // Get post views within the interval. | |
| $interval = ""; | |
| switch( $args['popular_range'] ){ | |
| case "daily": | |
| $interval = "1 DAY"; | |
| break; | |
| case "weekly": | |
| $interval = "1 WEEK"; | |
| break; | |
| case "monthly": | |
| $interval = "1 MONTH"; | |
| break; | |
| case "yearly": | |
| $interval = "1 YEAR"; | |
| break; | |
| default: | |
| $interval = "1 DAY"; | |
| break; | |
| } | |
| $where .= " AND v.last_viewed > DATE_SUB('{$now}', INTERVAL {$interval}) "; | |
| } | |
| } | |
| else { | |
| $from .= " {$prefix}posts p"; | |
| $from .= " LEFT JOIN {$prefix}term_relationships tr ON tr.object_id = p.ID"; | |
| $from .= " LEFT JOIN {$prefix}term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id"; | |
| $from .= " LEFT JOIN {$prefix}terms t ON t.term_id = tt.term_taxonomy_id"; | |
| $orderby .= " p.post_date DESC"; | |
| } | |
| if ($args['type']) { | |
| $type = esc_sql($args['type']); | |
| $where .= " AND p.post_type = '" . $type . "'"; | |
| } | |
| // No password. | |
| $where .= " AND p.post_password = ''"; | |
| // Published. | |
| $where .= " AND p.post_status = 'publish'"; | |
| $groupby .= " p.ID"; | |
| $limit = ""; | |
| $offset = ""; | |
| if ($args['posts_per_page']) { | |
| $posts_per_page = esc_sql($args['posts_per_page']); | |
| $limit = " LIMIT " . $posts_per_page; | |
| } | |
| if ($args['paged']) { | |
| $paged = esc_sql($args['paged']); | |
| $offset = " OFFSET " . $posts_per_page * ($paged -1); | |
| } | |
| $query = "{$select}{$from}{$where}{$groupby}{$having}{$orderby}{$limit}{$offset};"; | |
| $result = $wpdb->get_results($query); | |
| $total = $wpdb->get_var("SELECT FOUND_ROWS();"); | |
| $wpp_posts = array(); | |
| foreach ($result as $aPost) { | |
| array_push($wpp_posts, get_post($aPost->id)); | |
| } | |
| return array( | |
| 'posts' => $wpp_posts, | |
| 'total' => $total, | |
| 'query' => $query, | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment