[WordPress][プラグイン]WordPress Popular Posts
プラグインWordPress Popular Postsによって閲覧数が保存されている時
指定投稿タイプの記事IDを閲覧数の多い順に取得する
public function get_post_ids_order_by_views_wppp($post_type = 'post', $param = array()) { $param = array_merge( array( 'posts_per_page' => 5, 'range' => '1 MONTH', 'return_posts' => false, ), $param ); global $wpdb; $now = current_time('mysql'); $interval = ""; switch ($param['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 = $param['range']; break; } $sql[] = "SELECT postid AS ID,SUM(pageviews) AS views,p.post_type"; $sql[] = "FROM {$wpdb->prefix}popularpostssummary AS pps"; $sql[] = "LEFT JOIN $wpdb->posts AS p ON pps.postid = p.ID"; $sql[] = "WHERE view_datetime > DATE_SUB('{$now}', INTERVAL $interval)"; $sql[] = "AND p.post_type " . (is_array($post_type) ? "IN ('" . implode("','", $post_type) . "')" : "= '$post_type'"); $sql[] = "AND p.post_status = 'publish'"; if (!empty($param['post_id'])) { $sql[] = "AND postid " . (is_array($param['post_id']) ? "IN (" . implode(",", $param['post_id']) . ")" : "= {$param['post_id']}"); } if (!empty($param['not_post_id'])) { $sql[] = "AND postid " . (is_array($param['not_post_id']) ? "NOT IN (" . implode(",", $param['not_post_id']) . ")" : "!= {$param['not_post_id']}"); } $sql[] = "GROUP BY postid"; $sql[] = "ORDER BY views DESC"; if (!empty($param['ppp'])) { $sql[] = "LIMIT 0,{$param['ppp']}"; }elseif (!empty($param['posts_per_page'])) { $sql[] = "LIMIT 0,{$param['posts_per_page']}"; } $posts = $wpdb->get_results(implode(' ', $sql)); if ($param['return_posts']) return $posts; $ids = array(); foreach ($posts as $post) { $ids[] = $post->ID; } return $ids; }