【WordPress】人気の記事をプラグインなしで実装してみた【超高速】
更新日:

ルナリス
定番の人気記事をプラグインなしで実装したわ
確かに色々なサイトで見かけるねー
ソルト
ルナリス
いかに処理を軽くするか考えて作った力作だからみんな使ってね
通常の3倍早そう!?!
ソルト
実装例
ランキング機能実装
PV数の保存・ランキング作成をするプログラムです。
functions.phpに記載をして下さい。
/**
* PVランキング作成
*/
//PV数カウント
function my_count_pv() {
$postID = get_the_ID();
$count = get_post_meta($postID, 'my_pv_count', true);
if( is_null($count) || !is_numeric( $count ) ){
$count = 0;
delete_post_meta($postID, 'my_pv_count');
add_post_meta($postID, 'my_pv_count', '0');
}else{
$count++;
}
update_post_meta($postID, 'my_pv_count', $count);
}
//PVランキング作成
add_option( 'my_pv_rank', '' ,'no');
function create_pv_rank(){
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'meta_key' => 'my_pv_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ){
$postIDs = array();
while ( $the_query->have_posts() ){
$the_query->the_post();
array_push( $postIDs, get_the_ID() );
}
}
wp_reset_postdata();
if( !empty( $postIDs ) ){
update_option('my_pv_rank', $postIDs ,'no');
}
// View数のリセット
delete_post_meta_by_key( 'my_pv_count' );
}
//1日ごとに人気ランキング作成
add_action('my_daily_event01', 'create_pv_rank');
function set_my_daily_event01() {
if ( !wp_next_scheduled( 'my_daily_event01' ) ) {
wp_schedule_event(time(), 'daily', 'my_daily_event01');
}
}
add_action('wp', 'set_my_daily_event01');
/**
* ウィジェットの追加
*/
//ウィジェットの登録
add_action( 'widgets_init', 'add_my_pv_rank' );
function add_my_pv_rank() {
register_widget( 'my_pv_rank_widget' );
}
class my_pv_rank_widget extends WP_Widget {
function __construct() {
parent::__construct(
'my_pv_rank_widget', // ID
__('人気記事'), // タイトル
array('description' => __( '人気記事を表示します。'), ) // 説明文
);
}
//ウィジェットの出力
public function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) {
echo $before_title . esc_html( $title ) . $after_title;
};
$pv_ranking = get_option( 'my_pv_rank' , null );
if( $pv_ranking ){
$args = array(
'post_type' => 'post',
'post__in' => $pv_ranking,
'posts_per_page' => 5,
'order' => 'ASC',
'orderby' => 'post__in'
);
$the_query = new WP_Query( $args );
}else{
$the_query = new WP_Query();
}
?>
<?php if ( $the_query->have_posts() ) : ?>
<ol class="my_pv_rank">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<div class="my_pv_rank_img">
<a href="<?php the_permalink(); ?>">
<img loading="lazy" src="<?php the_post_thumbnail_url('thumbnail'); ?>" width="100" height="100" alt="<?php the_title(); ?>">
</a>
</div>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endwhile;?>
</ol>
<?php else: ?>
<ol class="my_pv_rank">
<li>現在集計中です。</li>
</ol>
<?php endif; ?>
<?php wp_reset_postdata();
}
//管理画面の表示
public function form($instance) {
$defaults = array(
'title' => '',
);
$instance = wp_parse_args( (array) $instance, $defaults );
$title = $instance['title'];
?>
<p>タイトル:
<input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
//管理画面の更新
public function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
return $instance;
}
}
CSS
共通のCSSファイルや、サイドバーのCSSに記述して下さい。
.my_pv_rank { list-style: none; }
.my_pv_rank li { list-style: none; zoom: 1; margin-bottom: 15px; border: solid 1px #000; background: #fff; padding: 6px; }
.my_pv_rank li::after { content: ""; display: block; clear: both; }
.my_pv_rank_img { width: 80px; height: 80px; float: left; overflow: hidden; margin: 6px 12px 0 0; border: solid 1px; }
.my_pv_rank_img img { height: 80px; object-fit: none; }
.my_pv_rank a { color: #000; display: block; float: left; max-width: 186px; font-size: 13px; }
@media only screen and (max-width: 960px){
.my_pv_rank a { float: none; max-width: none; }
}
PVカウント処理
記事ページのテンプレートに記載して下さい。
// get_header()の下などできるだけ上に記述 <?php my_count_pv();//PV数保存 ?>
その他
関数名のmy_の部分を好きな名前に変更して使って下さい。
簡単な解説
- 記事ごとにPV数を保存(カスタムフィールド)
- wp-cronで1日1回PVランキングを作成
- ランキング作成後に記事のPV数をリセット
- PVランキングをもとにウィジェットで人気記事を表示
ルナリス
ウィジェットから使えるわ。1日1回の処理だから軽いのよ
使い方
ウィジェットから人気記事をサイドバーに登録することで自動的に表示されます。

・表示例

まとめ
今回はプラグインなしで人気記事の表示を実装してみました。
プラグインを使うとサイトが重くなってしまうといったデメリットがあったため、動作の軽さを重視して開発しました。
人気記事を表示したい方はぜひ参考にして下さい。
コメント一覧