bnomei / kirby3-feed

Generate a RSS/JSON-Feed and Sitemap from a Pages-Collection.
https://forum.getkirby.com/t/kirby3-feed-rss-json-sitemap/23574
MIT License
64 stars 7 forks source link

Can't get feed to return simple reverse published order with more than one post per day #16

Closed Splendorr closed 5 years ago

Splendorr commented 5 years ago

Hi! I really appreciate this plugin and am using it currently on a personal blog. I'm just having trouble with the sort order, in what seems like a straightforward scenario: I want the feed to sort by listed()->flip(). I just want to see the posts, from newest to oldest, according to their numerical post order.

(I've tried sorting by date and time, but for certain reasons I'd like to just sort my posts by their Kirby folder number order.)

I've had some trouble getting this simple-seeming configuration working in a few places, and it seems to relate to having more than one post per day. Here's what I'm seeing:

I have 3 posts, each set to Public, which I named first, second, and third, in the order they were created and published. You can see their folders are in order (25, 26, 27) and their titles correspond.

Screen Shot 2019-08-12 at 5 21 28 PM

In my template, I'm using <?php $articles = $page->children()->listed()->flip()->paginate(10) ?> to display the posts in flipped order. This works in the template:

Screen Shot 2019-08-12 at 5 23 17 PM

However, when I use $feed = page('posts')->children()->listed()->flip()->limit(20)->feed($options); for the feed, it returns the following:

Screen Shot 2019-08-12 at 5 24 35 PM
<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom">

  <channel>
    <title>POSTS by Nick Splendorr</title>
    <link>//localhost:3000/posts</link>
    <lastBuildDate>Mon, 12 Aug 2019 00:00:00 +0000</lastBuildDate>
    <atom:link href="//localhost:3000" rel="self" type="application/rss+xml" />

        <description>The most recent POSTS.</description>

        <item>
      <title>added first</title>
      <link>//localhost:3000/posts/added-first</link>
      <guid>posts/added-first</guid>
      <pubDate>Mon, 12 Aug 2019 00:00:00 +0000</pubDate>
      <description><![CDATA[]]></description>
    </item>
        <item>
      <title>added second</title>
      <link>//localhost:3000/posts/added-second</link>
      <guid>posts/added-second</guid>
      <pubDate>Mon, 12 Aug 2019 00:00:00 +0000</pubDate>
      <description><![CDATA[]]></description>
    </item>
        <item>
      <title>added third</title>
      <link>//localhost:3000/posts/added-third</link>
      <guid>posts/added-third</guid>
      <pubDate>Mon, 12 Aug 2019 00:00:00 +0000</pubDate>
      <description><![CDATA[]]></description>
    </item>
        <item>
      <title>&#8220;hungry for the longest time&#8221;</title>
      <link>//localhost:3000/posts/hungry-for-the-longest-time</link>
      <guid>posts/hungry-for-the-longest-time</guid>
      <pubDate>Mon, 05 Aug 2019 00:00:00 +0000</pubDate>
      <description><![CDATA[<figure><img alt="" src="//localhost:3000/media/pages/posts/hungry-for-the-longest-time/295067646-1565641417/e270ac4c-3f75-4869-9850-f467e9a13b03-2000x1499-q90.jpg"></figure>]]></description>
    </item>

Now, you can see the last item is from an earlier date, in the right order. I can see in the feed output that the problem is probably related to all of their pubDates being identical... but they are being output in the wrong order somehow vs the way flip() works in the template.

Do you have any advice or tips? Thanks for your time either way!

Splendorr commented 5 years ago

Here's my entire feed config.php in case that's helpful:

[
      'pattern' => 'posts/feed',
      'method' => 'GET',
      'action'  => function () {
          $options = [
              'title'       => 'POSTS by Nick Splendorr',
              'description' => 'The most recent POSTS.',
              'link'        => 'posts',
              'textfield'   => 'text',
          ];
          $feed = page('posts')->children()->listed()->flip()->limit(20)->feed($options);
          return $feed;
      }
]
bnomei commented 5 years ago

i will take a look today.

bnomei commented 5 years ago

since this plugin is more or less just a port of the k2 plugin from @bastianallgeier i kept the sorting by datefield logic:

https://github.com/bnomei/kirby3-feed/blob/master/classes/Feed.php#L83

since you want to sort using the num you should use 'datefield' => 'num', in the options array.

see https://getkirby.com/docs/reference/objects/page/num

you could use sort instead if num but that would return the num anyway.

https://getkirby.com/docs/reference/objects/page/sort#no-parameter

bnomei commented 5 years ago

note: if there is no field called date the modified timestamp will be used.

bnomei commented 5 years ago

with version 1.3.0 you can now disable the build in sorting in adding the 'sort'=> false to the array of options you forward the ->feed($options).