WordPress Popular Postsを使い、特定のタグがつけられた記事を人気順に並べる

 

WordPress Popular Posts は便利なワードプレスのプラグインで、記事を人気順に並べることが可能です。

 

特定のカテゴリーに含まれる記事だけを表示することはできるのですが、タグでは表示を絞る事ができません。

というか、人気記事を表示させるプラグインの、ほとんどは何故かタグで表示を絞ることはできません。

目次

特定のタグが含まれる記事を人気順で並べるには

WordPress Popular Posts プラグインのwordpress-popular-postsの、form.php の中に追記することで実現可能です。

ちなみに修正したのはVersion: 3.3.2 です。

//wordpress-popular-posts.phpの169行目辺り
protected $defaults = array(
	'title' => '',
	'limit' => 10,
	'range' => 'daily',
	'freshness' => false,
	'order_by' => 'views',
	'post_type' => 'post,page',
	'pid' => '',
	'author' => '',
	'cat' => '',
	'taxonomy' => '',//追加



//wordpress-popular-posts.phpの464行目辺り
	echo "\n". "<!-- WordPress Popular Posts Plugin v{$this->version} [W] [{$instance['range']}] [{$instance['order_by']}] [{$markup}]" . ( !empty($instance['pid']) ? " [PID]" : "" ) . ( !empty($instance['cat']) ? " [CAT]" : "" ) . ( !empty($instance['taxonomy']) ? " [taxonomy]" : "" ) . ( !empty($instance['author']) ? " [UID]" : "" ) . " -->" . "\n";



//wordpress-popular-posts.phpの542行目辺り
	$instance['pid'] = implode(",", array_filter(explode(",", preg_replace( '|[^0-9,]|', '', $new_instance['pid'] ))));
	$instance['cat'] = implode(",", array_filter(explode(",", preg_replace( '|[^0-9,-]|', '', $new_instance['cat'] ))));
	$instance['taxonomy'] = implode(",", array_filter(explode(",", preg_replace( '|[^0-9,-]|', '', $new_instance['taxonomy'] )))); //追加
	$instance['author'] = implode(",", array_filter(explode(",", preg_replace( '|[^0-9,]|', '', $new_instance['uid'] ))));
	$instance['shorten_title']['words'] = $new_instance['shorten_title-words'];
	$instance['shorten_title']['active'] = isset( $new_instance['shorten_title-active'] );


//wordpress-popular-posts.phpの1476行目辺り
	$post_types = "";
	$pids = "";
	$cats = "";
	$taxonomys = ""; // 追加
	$authors = "";
	$content = "";


//wordpress-popular-posts.phpの1547行目あたりに追加
// * taxonomys
if ( !empty($instance['taxonomy']) ) {
    $taxonomy_ids = explode(",", $instance['taxonomy']);
    $in = array();
    $out = array();
    $not_in = "";


    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
        $where .= " 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
        $where .= " 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
        $where .= " 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)
            ) ";
    }
}
// * taxonomys


//wordpress-popular-posts.phpの1547行目あたり
	extract( shortcode_atts( array(
				'header' => '',
				'limit' => 10,
				'range' => 'daily',
				'freshness' => false,
				'order_by' => 'views',
				'post_type' => 'post,page',
				'pid' => '',
				'cat' => '',
				'taxonomy' => '',   // 追加
				'author' => '',
				'title_length' => 0,
				'title_by_words' => 0,
				'excerpt_length' => 0,
				'excerpt_format' => 0,
				'excerpt_by_words' => 0,
				'thumbnail_width' => 0,
				'thumbnail_height' => 0,
				'rating' => false,
				'stats_comments' => false,
				'stats_views' => true,
				'stats_author' => false,
				'stats_date' => false,
				'stats_date_format' => 'F j, Y',
				'stats_category' => false,
				'wpp_start' => '<ul class="wpp-list">',
				'wpp_end' => '</ul>',
				'header_start' => '<h2>',
				'header_end' => '</h2>',
				'post_html' => '',
				'php' => false
			),$atts));



//wordpress-popular-posts.phpの2908行目あたり
	$shortcode_content = "\n". "<!-- WordPress Popular Posts Plugin v". $this->version ." [" . ( $php ? "PHP" : "SC" ) . "] [".$shortcode_ops['range']."] [".$shortcode_ops['order_by']."] [custom]" . ( !empty($shortcode_ops['pid']) ? " [PID]" : "" ) . ( !empty($shortcode_ops['cat']) ? " [CAT]" : "" ) . ( !empty($shortcode_ops['author']) ? " [UID]" : "" ) . " -->"."\n";


//上記を下記に置き換える
	$shortcode_content = "\n". "<!-- WordPress Popular Posts Plugin v". $this->version ." [" . ( $php ? "PHP" : "SC" ) . "] [".$shortcode_ops['range']."] [".$shortcode_ops['order_by']."] [custom]" . ( !empty($shortcode_ops['pid']) ? " [PID]" : "" ) . ( !empty($shortcode_ops['cat']) ? " [CAT]" : "" ) . ( !empty($shortcode_ops['taxonomy']) ? " [taxonomy]" : "" ) . ( !empty($shortcode_ops['author']) ? " [UID]" : "" ) . " -->"."\n";




//wordpress-popular-posts.phpの43行目あたり
	<input type="text" id="<?php echo $this->get_field_id( 'cat' ); ?>" name="<?php echo $this->get_field_name( 'cat' ); ?>" value="<?php echo $instance['cat']; ?>" class="widefat" /><br /><br />

	<label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><?php _e('Tag(s) ID(s)', 'wordpress-popular-posts'); ?>:</label>
	<input type="text" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>" value="<?php echo $instance['taxonomy']; ?>" class="widefat" /><br /><br /> //追加

	<label for="<?php echo $this->get_field_id( 'uid' ); ?>"><?php _e('Author(s) ID(s)', 'wordpress-popular-posts'); ?>:</label> 

 

これで、下記のようにウィジェット内にTag(s) ID(s) という項目が出来上がります。

あとは、通常のWordPress Popular Posts設定方法でOKです。

 

設定周りで悩んだら、お気軽にご相談ください。
問合せの際は「web担当者を見た」とお伝えください。

この記事を書いた人

デジタルマーケティングに16年間従事しているMITSUIです。Google AnalyticsとGoogle Tag Managerが大好きで、これらのツールを活用した情報提供を行っています。ブログではデジタルマーケティングに関する情報や最新のトレンド、ベストプラクティスを紹介しています。

コメント

コメントする

CAPTCHA


目次