<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>広島、晴れのち晴れ &#187; コメントのみ</title>
	<atom:link href="http://tico-jpn.com/tag/%e3%82%b3%e3%83%a1%e3%83%b3%e3%83%88%e3%81%ae%e3%81%bf/feed" rel="self" type="application/rss+xml" />
	<link>http://tico-jpn.com</link>
	<description>広島から送るティコさんの日常とかサンフレッチェ広島とか、たまにTHE BACK HORN、低確率でWordPressの話題が出てくるブログです。</description>
	<lastBuildDate>Thu, 09 Feb 2012 09:10:26 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tico-jpn.com/tag/%e3%82%b3%e3%83%a1%e3%83%b3%e3%83%88%e3%81%ae%e3%81%bf/feed" />
		<item>
		<title>コメントのみ取得する関数</title>
		<link>http://tico-jpn.com/480/get-only-comments</link>
		<comments>http://tico-jpn.com/480/get-only-comments#comments</comments>
		<pubDate>Thu, 15 Jan 2009 11:26:39 +0000</pubDate>
		<dc:creator>ティコ</dc:creator>
				<category><![CDATA[WordPress備忘録]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[コメントのみ]]></category>
		<category><![CDATA[関数]]></category>

		<guid isPermaLink="false">http://tico-jpn.com/?p=480</guid>
		<description><![CDATA[WordPressでコメントのみを取得する関数を作成しました。取得するデータはデータベースから直接取ってあるので、自由に加工、表示ができます。]]></description>
			<content:encoded><![CDATA[<p>WordPressでコメントのみを取得する関数を作成してみました。</p>
<pre>
function get_only_comments( $args = &#039;&#039; ) {
	global $wpdb;

	$defaults = array(&#039;limit&#039; =&gt; 10, &#039;post_id&#039; =&gt; 0, &#039;order&#039; =&gt; &#039;DESC&#039;, &#039;approved&#039; =&gt; 1);
	$args = wp_parse_args($args, $defaults);

	if($args[&#039;limit&#039;] &lt;= 0 || $args[&#039;limit&#039;] &gt; 1000) {
		$limit = 1000;
	} else {
		$limit = $args[&#039;limit&#039;];
	}

	$postid_sql = &quot;&quot;;
	if($args[&#039;post_id&#039;] &gt; 0) {
		$postid_sql = &quot; AND comment_post_ID=&quot; . $args[&#039;post_id&#039;];
	}

	if($args[&#039;order&#039;] != &quot;DESC&quot; &amp;&amp; $args[&#039;order&#039;] != &quot;ASC&quot;) {
		$order = &quot;DESC&quot;;
	} else {
		$order = $args[&#039;order&#039;];
	}

	$approved_sql = &quot;&quot;;
	if($args[&#039;approved&#039;] == 1) {
		$approved_sql = &quot; AND comment_approved=1&quot;;
	}

	$comments = $wpdb-&gt;get_results(&quot;SELECT comment_approved, comment_author, comment_author_url, comment_ID, comment_post_ID, comment_content, comment_date FROM $wpdb-&gt;comments WHERE comment_type=&#039;&#039;$postid_sql$approved_sql ORDER BY comment_date $order LIMIT $limit&quot;);

	return $comments;
}
</pre>


<p>
functions.phpなんかに入れてやればどこからでも使えます。
</p>
<h3>引数</h3>
<p>引数を複数指定する場合は「&#038;」で繋ぎます。</p>
<dl>
<dt>limit</dt><dd>取得するコメントの数を指定(0を指定した場合は最大で1000コメントを取得)<br />デフォルトは10</dd>
<dt>post_id</dt><dd>取得するコメントの記事id(0を指定した場合は全てのコメントを取得)<br />デフォルトは0</dd>
<dt>order</dt><dd>日付でのコメント並び順(ASC　/　DESC)<br />デフォルトはDESC(降順)</dd>
<dt>approved</dt><dd>承認されているコメントのみ取得(1　/　0)<br />デフォルトは1(承認済みのみ取得)</dd>
</dt>
</dl>
<h3>取得するデータ</h3>
<p>各コメントデータが配列で取得できます。</p>
<ul>
<li>comment_approved : 未承認=0, 承認された=1</li>
<li>comment_author : コメント作成者の名前</li>
<li>comment_author_url : コメント作成者のURL</li>
<li>comment_ID : コメントID</li>
<li>comment_post_ID : コメントが投稿された記事ID</li>
<li>comment_content : コメント本文</li>
<li>comment_date : コメントされた日付</li>
</li>
</ul>
<h3>使用例</h3>
<pre>
/*
 コメントデータ取得について
*/

// 承認済みのコメントで最新10件を取得
$comment_array = get_only_comments();

// 記事IDを指定
$comment_array = get_only_comments('post_id=' . $post->ID);

// 取得するコメント数を指定
$comment_array = get_only_comments('limit=5');

// 記事ID=100のコメントを日付の昇順で全て取得（未承認も含む）
$comment_array = get_only_comments('limit=0&#038;post_id=100&#038;order=ASC&#038;approved=0');


/*
 取得したデータの活用についての例
*/

// コメント数の表示
echo count($comment_array);

// 配列のループ
foreach($comment_array as $comment) :
	// コメントが承認されているかの確認
	if($comment->comment_approved == 1) :

		// コメントが投稿された記事のタイトルを表示
		echo get_the_title($comment->comment_post_ID);

		// 記事のリンク先を表示
		echo get_permalink($comment->comment_post_ID);

		// コメント本文を30文字まで表示
		echo mb_substr($comment->comment_content, 0, 30);

		// コメント本文を改行を付けて表示
		echo nl2br($comment->comment_content);

	endif;
endforeach;
</pre>


<p>
「プラグインを導入するまでもない」と思っている方や「データの出力を細かく自分で制御したい」方には有用かと思ったので載せておきます。SQLが分かる方であれば関数のSQL文を変更して色々自分好みのデータが取得できます。
</p>
<p>ちなみにトラックバックのみを取得したい場合は関数内の「WHERE comment_type=&#8221;」を「WHERE comment_type!=&#8221;」にすれば良いはず！</p>
]]></content:encoded>
			<wfw:commentRss>http://tico-jpn.com/480/get-only-comments/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://tico-jpn.com/480/get-only-comments" />
	</item>
	</channel>
</rss>

