如果您的日期以以下格式保存,Y-m-d
, 这很容易。您的年份将保持不变,因此您可以按月份进行技术排序。
只需将正确的值添加到order
和orderby
参数。您可以尝试以下方法
// Query Post Type
$args = array(
\'post_type\' => \'events\',
\'post_status\' => \'publish\',
\'meta_key\' => \'_eDate\',
\'orderby\' => \'meta_value\'
);
编辑,因为您已在中的日期之前完成排序
_eDate
, 在同一个月内通过一组帖子显示月数很容易。你只需要比较一下之前的帖子
_eDate
使用当前帖子的
_eDare
, 如果不匹配,则输出月份名称。
示例:
// Query Post Type
$args = array(
\'post_type\' => \'events\',
\'post_status\' => \'publish\',
\'meta_key\' => \'_eDate\',
\'orderby\' => \'meta_value\'
);
$the_query = new WP_Query($args);
// Build It
if ( $the_query->have_posts() ) {
$month_array = array();
while ( $the_query->have_posts() ) {
$the_query->the_post();
$date_field = get_post_meta( $post->ID, \'_eDate\', true );
$month_format = DateTime::createFromFormat( \'Y-m-d\', $date_field );
$month_number = $month_format->format(\'m\');
$month_array[] = $month_number;
// Choose the format you want to display as indicated below
if ( $the_query->current_post == 0 ) // Output date id this is the first post
echo \'<h2>\' . $month_format->format( \'F\' ) . \'</h2>\'; // Output month name as January
if ( $the_query->current_post != 0
&& $month_number != $month_array[$the_query->current_post - 1]
)
echo \'<h2>\' . $month_format->format( \'F\' ) . \'</h2>\'; // Output month name as January
// DISPLAY YOUR POSTS AS PER NORMAL
} // endwhile
wp_reset_postdata();
} // endif