简体   繁体   English

WordPress循环按自定义日期排序

[英]WordPress loop sort by custom date

I have an events custom post type, but would like to sort the posts by date, which is a custom field. 我有一个事件自定义帖子类型,但是想按日期对帖子进行排序,这是一个自定义字段。 I've tried a few methods using meta in the $args but can't get it right. 我已经尝试了一些在$ args中使用meta的方法,但无法正确完成。 Does anybody have any suggestions? 有人有什么建议吗?

<?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; ?>

I'm using variable to split the date as you can see, thank you! 如您所见,我正在使用变量拆分日期,谢谢!

Save your date in database using strtotime() and you will storage int value of date. 使用strtotime()将日期保存在数据库中,您将存储日期的int值。 And this should work 这应该工作

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

This method works fine for me. 这种方法对我来说很好。

If you have date 'from' and date 'to' - save it to different fields. 如果您有日期“从”和日期“至”-将其保存到其他字段。

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

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