サポート » 使い方全般 » 投稿日ごとに投稿タイプ別の記事を表示したい

  • お世話になっています。

    タイトルだけだと若干わかりにくいのですが、下記のような表示をしたいと思っています。

    <div class="section">
    <h3>2月8日</h3>
    
    <h4>2月8日は○○の日</h4>
    <ul>
    <li><a href="リンク">「投稿」のタイトル1</a></li>
    <li><a href="リンク">「投稿」のタイトル2</a></li>
    </ul>
    
    <ul>
    <li><a href="リンク">「投稿タイプA」のタイトル1</a></li>
    <li><a href="リンク">「投稿タイプA」のタイトル2</a></li>
    <li><a href="リンク">「投稿タイプA」のタイトル3</a></li>
    </ul>
    </div>
    <div class="section">
    <h3>2月10日</h3>
    
    <ul>
    <li><a href="リンク">「投稿」のタイトル1</a></li>
    <li><a href="リンク">「投稿」のタイトル2</a></li>
    </ul>
    
    <ul>
    <li><a href="リンク">「投稿タイプA」のタイトル1</a></li>
    </ul>
    </div>

    現状、投稿と投稿タイプAにある記事を取得することまでは出来たのですが、
    今のコードでは表示がまざってしまうため、この先からどう書けばいいのか・・・と手が止まってしまいました。

    前提として
    ・Advanced Custom Fieldで、投稿を取得する日を指定(指定した日から1ヶ月間の投稿を取得します)
    ・月のうち、特定の日だけ<h4>タグを出力したい
     ※こちらはget_date()などで日付を比較すれば出来そうなので、ひとまずスルーします

    現在のコードはこちらです。

    <?php function filter_where( $where = '' ) {
    	// カスタムフィールドで選択した月から1ヶ月の投稿を取得する
    	$where .= " AND post_date >= '" . get_field('start').'-00-00' . "'" . " AND post_date <= '" . date( 'Y-m-d-23-59',strtotime("+1 month" ,strtotime( get_field('start').'-23-59' )) ) . "'";
    	return $where;
    }
    
    add_filter( 'posts_where', 'filter_where' ); //フィルターを追加
    global $query_string;
    $args = array(
    	'post_type' => array('post', 'customtypeA'), //取得する投稿タイプ
    	'posts_per_page' => -1,
    	'order' => 'ASC',
    	'orderby' => 'date'
    );
    $count = 0;
    $myQuery = new WP_Query( $args ); //クエリまわす
    echo '<div class="section">';
    while( $myQuery->have_posts() ): $myQuery->the_post();
    $postdate = the_date('n/j', '<h3>', '</h3><ul>', false);
    
    if ($postdate != NULL && $count != 0) {
    	echo '</ul></div><div class="section">';
    }; ?>
    <?php echo $postdate; ?>
    <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li>
    
    <?php $count++;
    endwhile;wp_reset_postdata();
    echo '</ul></div>';
    
    remove_filter( 'posts_where', 'filter_where' ); //フィルターを削除(他のクエリに影響させないため)

    どうぞ、よろしくお願い致します。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 chiilog (Chiaki Okamoto)

    (@mel_cha)

    自己解決できました!一応コード置いていきます。

    function filter_where( $where = '' ) {
    	// カスタムフィールドで選択した月から1ヶ月の投稿を取得する
    	$where .= " AND post_date >= '" . get_field('start_calendar').'-00-00' . "'" . " AND post_date <= '" . date( 'Y-m-d-23-59',strtotime("+1 month" ,strtotime( get_field('start_calendar').'-23-59' )) ) . "'";
    	return $where;
    }
    
    add_filter( 'posts_where', 'filter_where' ); //フィルターを追加
    global $query_string;
    $arr_dates = array(); //日付を入れる配列
    $args = array(
    	'post_type' => array('post', 'customtypeA'), //取得する投稿タイプ
    	'posts_per_page' => -1, //全件取得
    	'order' => 'ASC',
    	'orderby' => 'date'
    );
    $myQuery = new WP_Query( $args ); //クエリまわす
    while( $myQuery->have_posts() ): $myQuery->the_post(); //日付の配列を作成
    $date_org = the_date('Y-m-d','','', false);
    array_push($arr_dates, $date_org);
    endwhile;wp_reset_postdata();
    $arr_dates = array_filter($arr_dates); //空の配列削除
    $arr_dates = array_merge($arr_dates); //添字振り直し
    
    foreach ($arr_dates as $arr_date) {
    	$year = date('Y', strtotime($arr_date)); //年
    	$month = date('m', strtotime($arr_date)); //月
    	$day = date('d', strtotime($arr_date)); //日
    	$today = date('n月j日', strtotime($arr_date)); //表示用の日付
    	echo '<div class="section">';
    	echo '<h3>'.$today.'</h3>';
    	$post_args = '&year='.$year.'&monthnum='.$month.'&day='.$day.'&posts_per_page=-1&order=ASC&orderby=date'; ?>
    
    <?php if($day == 17) : //特定の日に文字列(strtotime()したほうがいいのかな・・・)
    echo '<h4>ほげほげ~</h4>';
    endif; ?>
    
    <?php $date_query = new WP_Query( 'post_type=post'.$post_args ); //投稿のループ
    if ($date_query->have_posts()) : ?>
    <ul class="posttype1">
    <?php while ( $date_query->have_posts() ) : $date_query->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;wp_reset_postdata(); ?>
    </ul>
    <?php endif; ?>
    
    <?php $other_query = new WP_Query( 'post_type=customtypeA'.$post_args ); //カスタム投稿のループ
    if ($other_query->have_posts()) : ?>
    <ul class="posttype2">
    <?php while ( $other_query->have_posts() ) : $other_query->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;wp_reset_postdata(); ?>
    </ul>
    <?php endif; ?>
    
    <?php echo '</div>'; //end div.section
    }; //endforeach
    
    remove_filter( 'posts_where', 'filter_where' ); //フィルターを削除(他のクエリに影響させないため)
1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「投稿日ごとに投稿タイプ別の記事を表示したい」には新たに返信することはできません。