@@ -79,16 +79,38 @@ public function getPrevPost()
7979 {
8080 if ($ this ->_prevPost === null ) {
8181 $ this ->_prevPost = false ;
82+ $ currentPost = $ this ->getPost ();
8283 $ collection = $ this ->_getFrontendCollection ()->addFieldToFilter (
8384 'publish_time ' ,
84- [
85- 'gteq ' => $ this ->getPost ()->getPublishTime ()
86- ]
87- )
88- ->setOrder ('publish_time ' , 'ASC ' )
89- ->setPageSize (1 );
90-
91- $ post = $ collection ->getFirstItem ();
85+ ['gteq ' => $ currentPost ->getPublishTime ()]
86+ )->setOrder ('publish_time ' , 'ASC ' );
87+
88+ $ post = $ collection ->getFirstItem ();
89+
90+ if ($ currentPost ->getPublishTime () == $ post ->getPublishTime ()) {
91+
92+ $ collection = $ this ->_postCollectionFactory ->create ();
93+ $ collection ->addActiveFilter ()
94+ ->addStoreFilter ($ this ->_storeManager ->getStore ()->getId ())
95+ ->addFieldToFilter ('publish_time ' , $ currentPost ->getPublishTime ())
96+ ->setOrder ('post_id ' , 'DESC ' );
97+
98+ if ($ collection ->getFirstItem ()->getId () != $ currentPost ->getId ()) {
99+ foreach ($ collection as $ item ) {
100+ if ($ item ->getId () != $ currentPost ->getId ()) {
101+ $ post = $ item ;
102+ } else {
103+ break ;
104+ }
105+ }
106+ } else {
107+ $ collection = $ this ->_getFrontendCollection ()->addFieldToFilter (
108+ 'publish_time ' ,
109+ ['gt ' => $ this ->getPost ()->getPublishTime ()]
110+ );
111+ $ post = $ collection ->getFirstItem ();
112+ }
113+ }
92114
93115 if ($ post ->getId ()) {
94116 $ this ->_prevPost = $ post ;
@@ -106,25 +128,47 @@ public function getNextPost()
106128 {
107129 if ($ this ->_nextPost === null ) {
108130 $ this ->_nextPost = false ;
131+ $ currentPost = $ this ->getPost ();
109132 $ collection = $ this ->_getFrontendCollection ()->addFieldToFilter (
110133 'publish_time ' ,
111- [
112- 'lteq ' => $ this ->getPost ()->getPublishTime ()
113- ]
114- )
115- ->setOrder ('publish_time ' , 'DESC ' )
116- ->setPageSize (1 );
117-
118- $ post = $ collection ->getFirstItem ();
134+ ['lteq ' => $ currentPost ->getPublishTime ()]
135+ )->setOrder ('publish_time ' , 'DESC ' );
136+
137+ $ post = $ collection ->getFirstItem ();
138+
139+ if ($ currentPost ->getPublishTime () == $ post ->getPublishTime ()) {
140+
141+ $ collection = $ this ->_postCollectionFactory ->create ();
142+ $ collection ->addActiveFilter ()
143+ ->addStoreFilter ($ this ->_storeManager ->getStore ()->getId ())
144+ ->addFieldToFilter ('publish_time ' , $ currentPost ->getPublishTime ())
145+ ->setOrder ('post_id ' , 'ASC ' );
146+
147+ if ($ collection ->getFirstItem ()->getId () != $ currentPost ->getId ()) {
148+ foreach ($ collection as $ item ) {
149+ if ($ item ->getId () != $ currentPost ->getId ()) {
150+ $ post = $ item ;
151+ } else {
152+ break ;
153+ }
154+ }
155+ } else {
156+ $ collection = $ this ->_getFrontendCollection ()->addFieldToFilter (
157+ 'publish_time ' ,
158+ ['lt ' => $ this ->getPost ()->getPublishTime ()]
159+ );
160+ $ post = $ collection ->getFirstItem ();
161+ }
162+ }
119163
120164 if ($ post ->getId ()) {
121165 $ this ->_nextPost = $ post ;
122166 }
123167 }
124-
125168 return $ this ->_nextPost ;
126169 }
127170
171+
128172 /**
129173 * Retrieve post collection with frontend filters and order
130174 * @return bool
@@ -135,7 +179,7 @@ protected function _getFrontendCollection()
135179 $ collection ->addActiveFilter ()
136180 ->addFieldToFilter ('post_id ' , ['neq ' => $ this ->getPost ()->getId ()])
137181 ->addStoreFilter ($ this ->_storeManager ->getStore ()->getId ())
138- ->setOrder ('publish_time ' , 'DESC ' )
182+ // ->setOrder('publish_time', 'DESC')
139183 ->setPageSize (1 );
140184 return $ collection ;
141185 }
0 commit comments