繁体   English   中英

WordPress循环按自定义日期排序

[英]WordPress loop sort by custom date

我有一个事件自定义帖子类型,但是想按日期对帖子进行排序,这是一个自定义字段。 我已经尝试了一些在$ args中使用meta的方法,但无法正确完成。 有人有什么建议吗?

<?php

    $args = array(
        'post_type' => 'fw-event',
        'posts_per_page' => -1
    );

    $the_query = new WP_Query( $args );

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

        global $post;
        $options = fw_get_db_post_option($post->ID, fw()->extensions->get( 'events' )->get_event_option_id());
        $eventbrite = fw_get_db_post_option($post->ID, 'eventbrite');
        $eventlogo = fw_get_db_post_option($post->ID, 'eventlogo');
        $test = fw_get_db_post_option($post->ID, 'event_date_range');

        // Set variables for date
        if ($options['event_children']) {
            foreach($options['event_children'] as $key => $row) :
                $originalDate = $row['event_date_range']['from'];
                $newDate = date("d M, Y", strtotime($originalDate));

                $Day = date("d", strtotime($originalDate));
                $Month = date("M", strtotime($originalDate));
            endforeach;
        }

        ?>

        <?php if (strtotime($newDate) > time() && $options['event_children']) { ?>

        <div class="event-listing">
            <div class="date matchHeightRow">
                <?php if ($options['event_children']) { ?>
                    <?php foreach($options['event_children'] as $key => $row) : ?>
                        <span><?php echo esc_attr__($Day); ?></span>
                        <?php echo esc_attr__($Month); ?>
                    <?php endforeach; ?>
                <?php } ?>
            </div>
            <div class="content matchHeightRow">
                <aside>
                    <h5>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                            <?php the_title(); ?>
                        </a>
                    </h5>
                    <?php echo limit_words(get_the_excerpt(), '15'); ?>&hellip;
                    <p>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                            <?php echo esc_attr__( 'Find out more &rarr;' , 'wmmc' ); ?>
                        </a>
                    </p>
                </aside>
            </div>
        </div>

    <?php } ?>

<?php endwhile; ?>

如您所见,我正在使用变量拆分日期,谢谢!

使用strtotime()将日期保存在数据库中,您将存储日期的int值。 这应该工作

 $args = array(
    'post_type'      => 'fw-event',
    'posts_per_page' => - 1,
    'meta_key'       => 'my_date',
    'order'          => 'DESC',
    'orderby'        => 'meta_value_num',
);

这种方法对我来说很好。

如果您有日期“从”和日期“至”-将其保存到其他字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM