Сортировка php объекта по ближайшим датам - PullRequest
1 голос
/ 01 августа 2020

У меня есть php объект, получаемый новым WP_Query ($ query_args). Мне нужно отсортировать этот объект по ближайшей дате. У каждого события есть дата начала и дата окончания. Если дата начала события ближе к сегодняшнему дню, это событие должно быть первым в выводе.

Это мой объект в значении $ result.

            array(4) {
          [0]=>
          object(WP_Post)#3813 (24) {
            ["ID"]=>
            int(9905)
            ["post_author"]=>
            string(2) "31"
            ["post_date"]=>
            string(19) "2020-07-30 21:14:06"
            ["post_date_gmt"]=>
            string(19) "2020-07-30 21:14:06"
            ["post_content"]=>
            string(251) "ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad ad"
            ["post_title"]=>
            string(20) "Test 3 same location"
            ["post_excerpt"]=>
            string(0) ""
            ["post_status"]=>
            string(7) "publish"
            ["comment_status"]=>
            string(4) "open"
            ["ping_status"]=>
            string(6) "closed"
            ["post_password"]=>
            string(0) ""
            ["post_name"]=>
            string(20) "test-3-same-location"
            ["to_ping"]=>
            string(0) ""
            ["pinged"]=>
            string(0) ""
            ["post_modified"]=>
            string(19) "2020-07-30 21:15:32"
            ["post_modified_gmt"]=>
            string(19) "2020-07-30 21:15:32"
            ["post_content_filtered"]=>
            string(0) ""
            ["post_parent"]=>
            int(0)
            ["guid"]=>
            string(62) "https://www.stag.wediscover.club/listing/test-3-same-location/"
            ["menu_order"]=>
            int(0)
            ["post_type"]=>
            string(7) "listing"
            ["post_mime_type"]=>
            string(0) ""
            ["comment_count"]=>
            string(1) "0"
            ["filter"]=>
            string(3) "raw"
          }
          [1]=>
          object(WP_Post)#3812 (24) {
            ["ID"]=>
            int(9903)
            ["post_author"]=>
            string(2) "31"
            ["post_date"]=>
            string(19) "2020-07-29 20:32:40"
            ["post_date_gmt"]=>
            string(19) "2020-07-29 20:32:40"
            ["post_content"]=>
            string(167) "Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events Many events"
            ["post_title"]=>
            string(10) "Location 2"
            ["post_excerpt"]=>
            string(0) ""
            ["post_status"]=>
            string(7) "publish"
            ["comment_status"]=>
            string(4) "open"
            ["ping_status"]=>
            string(6) "closed"
            ["post_password"]=>
            string(0) ""
            ["post_name"]=>
            string(10) "location-2"
            ["to_ping"]=>
            string(0) ""
            ["pinged"]=>
            string(0) ""
            ["post_modified"]=>
            string(19) "2020-07-30 20:53:08"
            ["post_modified_gmt"]=>
            string(19) "2020-07-30 20:53:08"
            ["post_content_filtered"]=>
            string(0) ""
            ["post_parent"]=>
            int(0)
            ["guid"]=>
            string(52) "https://www.stag.wediscover.club/listing/location-2/"
            ["menu_order"]=>
            int(0)
            ["post_type"]=>
            string(7) "listing"
            ["post_mime_type"]=>
            string(0) ""
            ["comment_count"]=>
            string(1) "0"
            ["filter"]=>
            string(3) "raw"
          }
          [2]=>
          object(WP_Post)#3816 (24) {
            ["ID"]=>
            int(9901)
            ["post_author"]=>
            string(2) "31"
            ["post_date"]=>
            string(19) "2020-07-29 20:31:20"
            ["post_date_gmt"]=>
            string(19) "2020-07-29 20:31:20"
            ["post_content"]=>
            string(158) "Location test 1 Location test 1 Location test 1 Location test 1 Location test 1 Location test 1Location test 1 Location test 1 Location test 1 Location test 1"
            ["post_title"]=>
            string(15) "Location test 1"
            ["post_excerpt"]=>
            string(0) ""
            ["post_status"]=>
            string(7) "publish"
            ["comment_status"]=>
            string(4) "open"
            ["ping_status"]=>
            string(6) "closed"
            ["post_password"]=>
            string(0) ""
            ["post_name"]=>
            string(15) "location-test-1"
            ["to_ping"]=>
            string(0) ""
            ["pinged"]=>
            string(0) ""
            ["post_modified"]=>
            string(19) "2020-07-30 20:20:56"
            ["post_modified_gmt"]=>
            string(19) "2020-07-30 20:20:56"
            ["post_content_filtered"]=>
            string(0) ""
            ["post_parent"]=>
            int(0)
            ["guid"]=>
            string(57) "https://www.stag.wediscover.club/listing/location-test-1/"
            ["menu_order"]=>
            int(0)
            ["post_type"]=>
            string(7) "listing"
            ["post_mime_type"]=>
            string(0) ""
            ["comment_count"]=>
            string(1) "0"
            ["filter"]=>
            string(3) "raw"
          }
          [3]=>
          object(WP_Post)#3817 (24) {
            ["ID"]=>
            int(9747)
            ["post_author"]=>
            string(2) "29"
            ["post_date"]=>
            string(19) "2020-07-02 21:24:09"
            ["post_date_gmt"]=>
            string(19) "2020-07-02 21:24:09"
            ["post_content"]=>
            string(525) "Join our most popular surfing meetup in Los Angeles.

        Surfers with all experience levels welcome!

        Approximate schedule for the morning:

        8.45 – 9.00 Meeting next to Perry’s Cafe

        9.00 – 9.30 Surfing

        9.30 – 9.45 Coffe / Snacks

        9.45 – 10.15 Surfing

        10.15 – 10.30 Coffe/ Snacks

        10.30 – 11.00 Volleyball

        We advise you to bring hot coffee or tea with you to make this morning even more cozier after surfing)

        Feel free to contact us if you have any questions.

        We can’t wait to see you!"
            ["post_title"]=>
            string(40) "Surfing and Volleyball at Torrance beach"
            ["post_excerpt"]=>
            string(0) ""
            ["post_status"]=>
            string(7) "publish"
            ["comment_status"]=>
            string(4) "open"
            ["ping_status"]=>
            string(6) "closed"
            ["post_password"]=>
            string(0) ""
            ["post_name"]=>
            string(42) "surfing-and-volleyball-at-torrance-beach-4"
            ["to_ping"]=>
            string(0) ""
            ["pinged"]=>
            string(0) ""
            ["post_modified"]=>
            string(19) "2020-07-15 23:06:59"
            ["post_modified_gmt"]=>
            string(19) "2020-07-15 23:06:59"
            ["post_content_filtered"]=>
            string(0) ""
            ["post_parent"]=>
            int(0)
            ["guid"]=>
            string(84) "https://www.stag.wediscover.club/listing/surfing-and-volleyball-at-torrance-beach-3/"
            ["menu_order"]=>
            int(0)
            ["post_type"]=>
            string(7) "listing"
            ["post_mime_type"]=>
            string(0) ""
            ["comment_count"]=>
            string(1) "0"
            ["filter"]=>
            string(3) "raw"
          }
        }

Я решил свою проблему таким образом.

                $result = new WP_Query( $query_args );
            $sortResult = [];
            $keysArr = [];
            $eventStDate = "";
            $diffDays = 0;
            $interval = 0;
            $returningSortResult = $result;

            foreach ($result->posts as $ppkey => $ppvalue) {
                $eventStDate = get_post_meta($ppvalue->ID, "_event_date", false);
                $interval = date_diff(new DateTime(date( "Y-m-d",   strtotime($nowDate) )), new DateTime(date( "Y-m-d",   strtotime($eventStDate[0]) )));
                $sortResult[]["key"] = array($interval->d, $ppvalue);
            }
            asort($sortResult);
            $theKey = 0;
            foreach ($sortResult as $sortKey => $sortValue) {
                $keysArr[]["key"] = $sortKey;
                $returningSortResult->posts[$theKey] = $sortValue["key"][1];
                $theKey++;
            }

            return  $returningSortResult;

но я уверен, что есть лучшее решение. Я бы хотел улучшить эту сортировку.

извините за мой Engli sh:)

Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...