Loading [MathJax]/extensions/tex2jax.js

人気記事一覧をプラグイン無しで作る

サイドバーなどによく表示されている人気記事一覧。

記事へのアクセス数をカウントし、アクセス数が多い順に記事を一覧表示する方法をプラグイン無しで制作する方法を解説します。

今回制作するものは簡易的なものになり、グーグルアナリティクスとの連携などは行いませんのでズレは発生しますがある程度の目安になるでしょう。

functions.phpにカウントする処理を書く

functions.phpに以下のコードを記載してアクセス数取得とカウントを行う処理を制作します。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
//アクセス数を取得する処理
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
add_post_meta($postID, $count_key, 0, true);
return 0;
}
return $count;
}
//アクセスをカウントする処理
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
add_post_meta($postID, $count_key, 0, true);
} else {
$count++;
update_post_meta($postID, $count_key, $count);
}
}
//アクセス数を取得する処理 function getPostViews($postID){ $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ add_post_meta($postID, $count_key, 0, true); return 0; } return $count; } //アクセスをカウントする処理 function setPostViews($postID) { $count_key = 'post_views_count'; $count = get_post_meta($postID, $count_key, true); if($count==''){ add_post_meta($postID, $count_key, 0, true); } else { $count++; update_post_meta($postID, $count_key, $count); } }
//アクセス数を取得する処理
function getPostViews($postID){
  $count_key = 'post_views_count';
  $count = get_post_meta($postID, $count_key, true);
  if($count==''){
    add_post_meta($postID, $count_key, 0, true);
    return 0;
  }
  return $count;
}

//アクセスをカウントする処理
function setPostViews($postID) {
  $count_key = 'post_views_count';
  $count = get_post_meta($postID, $count_key, true);
  if($count==''){
    add_post_meta($postID, $count_key, 0, true);
  } else {
    $count++;
    update_post_meta($postID, $count_key, $count);
  }
}

カウントの仕組み

WordPressのデータベールのpost_metaデーブルのpost_views_count属性に開いている記事のpostIDのタプルが存在しない、または値がなければタプルの値を0でタプルを制作する。

もし値が存在するならその値に1を足し、値を保存しなおす。

これだけです。

WordPressのadd_post_metaタグ(関数リファレンス)を使用する事でデータベースに任意の属性名でデータを保存する事ができます。これはカスタムフィールドの値と同じテーブルに入ります。

今回の処理のイメージはpost_views_countという名前のカスタムフィールドを制作し、プログラムから内容をコントロールしているよいうようなイメージです。

WordPressのupdate_post_metaタグ(関数リファレンス)を使用する事でadd_post_metaタグで制作したデータの値をアップデートする事ができます。

WordPressのget_post_metaタグ(関数リファレンス)を使用する事で任意の値を取得することができます。

人気記事の表示方法

記事の取得は通常のクエリを発行して記事を取得します。

少し違うところは記事の並び順をpost_views_countの値でソートしている事です。

ランキング表示したいページのテンプレートに以下のコードを記載します。

sidebar.php

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
setPostViews($post->ID);//アクセス時に関数を実行でカウントする。
//記事取得用のクエリ
$args = array(
'post_status'=> 'publish',
'post_type'=> 'column',
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',//数字で並び替え
'order' => 'DESC',//閲覧数が多い順
'posts_per_page' => 5
);
setPostViews($post->ID);//アクセス時に関数を実行でカウントする。 //記事取得用のクエリ $args = array( 'post_status'=> 'publish', 'post_type'=> 'column', 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num',//数字で並び替え 'order' => 'DESC',//閲覧数が多い順 'posts_per_page' => 5 );
setPostViews($post->ID);//アクセス時に関数を実行でカウントする。

//記事取得用のクエリ
$args = array(
	'post_status'=> 'publish',
	'post_type'=> 'column',
	'meta_key' => 'post_views_count',
	'orderby' => 'meta_value_num',//数字で並び替え
	'order' => 'DESC',//閲覧数が多い順
	'posts_per_page' => 5
);

上記のコードでアクセス数が多い順に5つの記事を表示する事ができます。

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

プログラムちょと書ける。
いまはバリ島でスクール作っている。
プログラムちょっとできる。