WordPress Popular Postsでカスタムタクソノミーを抽出できるようにした

wordpress-popular-postsは、Wordpressのブログの人気記事の表示、表示回数の取得が出来るプラグインです。
結構広く使われていることと、似たようなプラグインであるWP-PostViewsが現在の最新のWordpress（3.8.1）の
カスタム投稿の表示回数の取得がどうもうまく行かなかったので使ってみました。

ところが、wordpress-popular-postsの人気記事の取得機能では、
カスタムタクソノミーで人気記事を絞る機能が無いことが判明。

こういうやりとり( 
http://wordpress.org/support/topic/custom-taxonomy-7
http://wordpress.org/support/topic/popular-posts-for-current-taxonomy
)はあったみたいなのですが、どうもまだ実装されてなかったようなので既存ソースにちょい足しで作成しました。

追加した対象は　WordPress Popular Posts の Version 2.3.7 です。

```php:wordpress-popular-posts.php

// 840行目付近
global $wpdb;
$table = $wpdb->prefix . "popularpostsdata";
$fields = "";
$from = "";
$where = "";
$post_types = "";
$pids = "";
$cats = "";
$taxonomys = ""; // add
$authors = "";
$content = "";

// 941行目付近に追加
// * taxonomys
if ( !empty($instance['taxonomy']) ) {
	$taxonomy_ids = explode(",", $instance['taxonomy']);
	$in = array();
	$out = array();
	$not_in = "";

	usort($taxonomy_ids, array(&$this, 'sorter'));

	for ($i=0; $i < count($taxonomy_ids); $i++) {
		if ($taxonomy_ids[$i] >= 0) $in[] = $taxonomy_ids[$i];
		if ($taxonomy_ids[$i] < 0) $out[] = $taxonomy_ids[$i];
	}

	$in_taxonomys = implode(",", $in);
	$out_taxonomys = implode(",", $out);
	$out_taxonomys = preg_replace( '|[^0-9,]|', '', $out_taxonomys );

	if ($in_taxonomys != "" && $out_taxonomys == "") { // get posts from from given taxonomys only
		$taxonomys = " 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.term_id IN($in_taxonomys)
			) ";
	} else if ($in_taxonomys == "" && $out_taxonomys != "") { // exclude posts from given taxonomys only
		$taxonomys = " 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.term_id IN($out_taxonomys)
			) ";
	} else { // mixed, and possibly a heavy load on the DB
		$taxonomys = " 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.term_id IN($out_taxonomys)
			) 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.term_id IN($out_taxonomys)
			) ";
	}
}


// 965行目
$from = " {$wpdb->posts} p LEFT JOIN {$table} v ON p.ID = v.postid WHERE {$post_types} {$pids} {$authors} {$cats} {$taxonomys} AND p.comment_count > 0 AND p.post_password = '' AND p.post_status = 'publish' ORDER BY p.comment_count DESC LIMIT {$instance['limit']} ";

// 968行目
$from = " {$wpdb->posts} p WHERE {$post_types} {$pids} {$authors} {$cats} {$taxonomys} AND p.comment_count > 0 AND p.post_password = '' AND p.post_status = 'publish' ORDER BY p.comment_count DESC LIMIT {$instance['limit']} ";

// 976行目
$from = " {$table} v LEFT JOIN {$wpdb->posts} p ON v.postid = p.ID WHERE {$post_types} {$pids} {$authors} {$cats} {$taxonomys} AND p.post_password = '' AND p.post_status = 'publish' ORDER BY pageviews DESC LIMIT {$instance['limit']} ";

// 981行目
$from = " {$table} v LEFT JOIN {$wpdb->posts} p ON v.postid = p.ID WHERE {$post_types} {$pids} {$authors} {$cats} {$taxonomys} AND p.post_password = '' AND p.post_status = 'publish' GROUP BY p.ID ORDER BY avg_views DESC LIMIT {$instance['limit']} ";

// 1025行目
$from .= " WHERE {$post_types} {$pids} {$authors} {$cats} {$taxonomys} AND p.post_password = '' AND p.post_status = 'publish' LIMIT {$instance['limit']} ";

// 1048行目
$from .= " WHERE {$post_types} {$pids} {$authors} {$cats} {$taxonomys} AND p.post_password = '' AND p.post_status = 'publish' ";

// 1926行目付近
extract( shortcode_atts( array(
	'header' => '',
	'limit' => 10,
	'range' => 'daily',
	'order_by' => 'views',
	'post_type' => 'post,page',
	'pid' => '',
	'cat' => '',
	'taxonomy' => '',	// add
	'author' => '',
	'title_length' => 0,

// 1966行目付近
$shortcode_ops = array(
	'title' => strip_tags($header),
	'limit' => empty($limit) ? 10 : (is_numeric($limit)) ? (($limit > 0) ? $limit : 10) : 10,
	'range' => (in_array($range, $range_values)) ? $range : 'daily',
	'order_by' => (in_array($order_by, $order_by_values)) ? $order_by : 'views',
	'post_type' => empty($post_type) ? 'post,page' : $post_type,
	'pid' => preg_replace( '|[^0-9,]|', '', $pid ),
	'cat' => preg_replace( '|[^0-9,-]|', '', $cat ),
	'taxonomy' => preg_replace( '|[^0-9,-]|', '', $taxonomy ),	// add
	'author' => preg_replace( '|[^0-9,]|', '', $author ),


```

** 使い方

```php
//パラメータにタクソノミーのIDを指定。
wpp_get_mostpopular("post_type=【カスタム投稿のname】&taxonomy=【タームのID】");
```