2018年3月1日

カスタムフィールドの「日」の値が早い順でさらに「時間」の値が早い順に並べて出力するWP Query

2018年3月1日 木曜日

イベント開催スケジュールを開催日の早い順でかつ同じ日で時間の早いイベント順に並べて出力する必要があったので、下記のようなWP Queryを書いて可能にしました。

<?php
$args = array(
        'meta_query' => array(
            'customfield_01' => array(
                'key' => 'start_date', //「日」のフィールド名
            ),
            'customfield_02' => array(
                'key' => 'start_time', //「時間」のフィールド名
            )
        ),
        'orderby' => array(
            'customfield_01' => 'ASC',
            'customfield_02' => 'ASC'
        )
    );

$the_query  = new WP_Query( $args );// ループ

if ( $the_query->have_posts() ) :

while ( $the_query->have_posts() ) : $the_query->the_post();

endwhile;
endif;
// 投稿データをリセット
wp_reset_postdata();
?>

(参考)
WP_Query get_posts 複数のカスタムフィールドの値で順序制御
Order by multiple meta keys in WP Query

ページトップへ