CPT GROUP BY DATE Metabox值

时间:2015-06-22 作者:Geoffrey

我正在使用自定义帖子类型构建一个事件插件。我运行了一个循环,它将使用一个短代码显示页面上的所有事件。我的问题是如何设置它,以便它按元数据库中设置的月份对事件进行分组_eDate 并将该组放在一个标题下。例如,7月的所有事件将显示在7月h1标记下。

以下是我当前使用的循环:

function events_list_shortcode() {

    // Query Post Type
    $args = array(
        \'post_type\' => \'events\',
        \'post_status\' => \'publish\'
);

$i = 0;
$the_query = new WP_Query($args);

// Build It
if ($the_query->have_posts()) :
    ?>
    <div id="event-list">
        <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
            <div class="row">
                <div class="event-image alignleft">
                    <a href="<?php echo get_permalink(get_the_ID()); ?>">
                        <?php
                        if (has_post_thumbnail()) {
                            the_post_thumbnail(\'thumbnail\');
                        }
                        ?>
                    </a>
                </div>
                <div class="event-content alignleft">
                    <h3><a href="<?php echo get_permalink(get_the_ID()); ?>"><?php the_title(); ?></a></h3>
                    <?php
                    $eventDate = strtotime(get_post_meta(get_the_ID(), \'_eDate\', true));

                    if (!empty($eventDate)) {
                        ?>
                        <div class="event-date">
                            <?php echo date(\'F - j\', $eventDate); ?>
                        </div>
                    <?php } ?>

                    <?php
                    $eventTime = strtotime(get_post_meta(get_the_ID(), \'_eTime\', true));

                    if (!empty($eventDate)) {
                        ?>
                        <div class="event-time">
                            <?php echo date(\'g:i A\', $eventTime); ?>
                        </div>
                    <?php } ?>

                    <?php
                    $eventPrice = get_post_meta(get_the_ID(), \'_ePrice\', true);

                    if (!empty($eventPrice)) {
                        ?>
                        <div class="event-price">
                            <?php echo $eventPrice; ?>
                        </div>
                    <?php } ?>

                    <br/>
                    <a href="<?php echo get_permalink(get_the_ID()); ?>">More Information</a>
                </div>
                <div class="event-buy alignleft">
                    <?php
                    $eventBuy = get_post_meta(get_the_ID(), \'_eBuy\', true);

                    if (!empty($eventDate)) {
                        ?>
                        <div class="event-buy">
                            <a class="buy-btn btn" href="<?php echo $eventBuy ?>" target="_blank">Buy Online</a>
                        </div>
                    <?php } ?> 
                </div>
            </div>
        </div>
        <?php wp_reset_postdata(); ?>
        <?php
    endwhile;
endif;
}

1 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

如果您的日期以以下格式保存,Y-m-d, 这很容易。您的年份将保持不变,因此您可以按月份进行技术排序。

只需将正确的值添加到orderorderby 参数。您可以尝试以下方法

// 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

结束

相关推荐