尝试按日期对一系列WordPress帖子进行排序,并在结尾处使用空值

时间:2019-01-03 12:36:01

标签: php wordpress sorting date

我正在尝试按开始日期对WordPress中的一系列帖子进行排序,这使用了自定义帖子类型。输入的日期是可选的,这意味着如果未输入日期,则将其视为null。

但是,日期格式未设置时会自动默认为1970年1月1日,这意味着无论如何,没有开始日期的帖子都将排在顶部而不是底部。反转顺序将不起作用,因为具有正确日期的事件将以错误的方式排序。

// This gets all the posts and sorts them by date

$events = get_posts([
    'post_type' => 'event',
    'posts_per_page' => -1,
    'meta_key' => 'eventStarttime',
    'orderby' => 'meta_value',
    'order' => 'ASC'
]);

// The events are looped through in this foreach

foreach ($events as $event) {
    echo "<button aria-controls='event-{$event->ID}' class='location-navigation-item js-location-button non-mobile'>";

    $timestamp = get_field('eventStartTime', $event->ID);
    $eventUnlocked = (get_field('eventActive') === "yes") ? true : false;
    $date = strtr($timestamp, '/', '-');

    echo "<h3>".strtoupper(get_the_title($event->ID))."</h3>";

    if ($timestamp):
        echo "<h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";
    else:
        echo "<h4 class='minorheading'>TBC</h4>";
    endif;

    echo "</button>";

    echo "<a href='".get_permalink($event->ID). "'>";
    echo "  <div class='location-navigation-item mobile'>";
    echo "    <div class='mobile-data'>";
    echo "      <h3>".strtoupper(get_the_title($event->ID))."</h3>";
    echo "      <h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";
    echo "    </div>";
    echo "    <button aria-controls='event-{$event->ID}' class='long-button event-location-button js-event-button'></button>";
    echo "  </div>";
    echo "</a>";
}

很抱歉缩进不良,

如您在时间戳记if语句上看到的那样,当未设置任何日期时,将列出“ TBC”作为值,如果未添加,则将显示1970年1月1日。

我只是希望将null事件放在结尾而不是开头,所以我在网上找不到任何内容。

关于代码的所有其他操作都有效,仅是顺序。

感谢您的时间

1 个答案:

答案 0 :(得分:0)

在任何情况下,两个循环都不是更快的解决方案

//The events are looped through in this foreach

                        foreach($events as $event)
                        {
                            echo "<button aria-controls='event-{$event->ID}' class='location-navigation-item js-location-button non-mobile'>";

                                $timestamp = get_field('eventStartTime', $event->ID);
                                $eventUnlocked = (get_field('eventActive') === "yes") ? true : false;
                                $date = strtr($timestamp, '/', '-');

                                if ($timestamp):
                                    echo "<h3>".strtoupper(get_the_title($event->ID))."</h3>";
                                    echo "<h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";


                                    echo "</button>";

                                    echo "<a href='".get_permalink($event->ID). "'>";
                                    echo "<div class='location-navigation-item mobile'>";
                                    echo "<div class='mobile-data'>";
                                        echo "<h3>".strtoupper(get_the_title($event->ID))."</h3>";
                                        echo "<h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";
                                    echo "</div>";
                                    echo "<button aria-controls='event-{$event->ID}' class='long-button event-location-button js-event-button'></button>";
                                    echo "</div>";
                                   echo "</a>";
                                endif;
                        }
reset ($events);
                        foreach($events as $event1)
                        {
                            echo "<button aria-controls='event1-{$event1->ID}' class='location-navigation-item js-location-button non-mobile'>";

                                $timestamp = get_field('eventStartTime', $event1->ID);
                                $eventUnlocked = (get_field('eventActive') === "yes") ? true : false;
                                $date = strtr($timestamp, '/', '-');

                                if (!$timestamp):
                                      echo "<h3>".strtoupper(get_the_title($event->ID))."</h3>";
                                      echo "<h4 class='minorheading'>TBC</h4>";


                                      echo "</button>";

                                      echo "<a href='".get_permalink($event1->ID). "'>";
                                      echo "<div class='location-navigation-item mobile'>";
                                      echo "<div class='mobile-data'>";
                                        echo "<h3>".strtoupper(get_the_title($event1->ID))."</h3>";
                                        echo "<h4 class='minorheading'>". date('d', strtotime($date)). '<sup>' . date('S', strtotime($date)) . '</sup>/' . strtoupper(date('M', strtotime($date)))."</h4>";
                                    echo "</div>";
                                    echo "<button aria-controls='event-{$event1->ID}' class='long-button event-location-button js-event-button'></button>";
                                echo "</div>";
                            echo "</a>";
                                endif;
                        }