Skip to content

Instantly share code, notes, and snippets.

@marushu
Created November 22, 2011 06:02
Show Gist options
  • Save marushu/1385012 to your computer and use it in GitHub Desktop.
Save marushu/1385012 to your computer and use it in GitHub Desktop.
get_postsを使って投稿を投稿日から指定した期間投稿を表示させる
<?php
$args = array (
'post_type' => 'one_line_comment',
'taxonomy' => 'span',
'post_per_page' => 5,
'order' => 'ASC',
'meta_key' => $disply_limit,
'meta_value' => $now,
'meta_compare' => '>'
);
$posts = get_posts($args);
foreach($posts as $post) {
setup_postdata($post);
// カスタムフィールド内の数値(日数)を取得
$key = '表示期間';
$date = get_post_meta($post->ID, '表示期間', true);
echo $date . '  表示期間の日数を表示<br />';
// 投稿日を取得する
$post_date = get_the_date('U');
echo $post_date . ' $post_date のこと。投稿日のUnixタイムスタンプ<br />';
// 数値(日数)をUnixタイムスタンプへ変更
$date_span = $date * 24 * 60 * 60;
echo $date_span . ' 表示期限のUnixタイムスタンプ<br />';
//投稿期限を設定する
$display_limit = $post_date + $date_span;
echo $display_limit . ' $display_limit のこと。表示させる期限のUnixタイムスタンプ<br />';
// サイトの「今日」の日付を取得
$now = current_time('timestamp', get_option('gmt_offset'));
echo $now . ' $now のこと。今日のUnixタイムスタンプ<br />';
//投稿期限と現在の日付の比較
if($display_limit > $now){
//投稿タイトルを表示
the_title('<h2>', '</h2>');
echo '<br />';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment