mitydigital/statamic-feedamic

Feature Request: Filter by taxonomy

Closed this issue · 6 comments

My client is wanting to have feeds that are specific to the categories that they send out.

It'd be great if I could either:

  • Create a feed that has collection set, AND optionally set the taxonomy filter. Eg collection: news + taxonomy: category = health
  • Alternatively, be able to set a filter in the view itself, meaning that I can have a /news/feed as my feed, but then allow the client to set query parameters in the feed for: ?newsCategories[0]=health for example.

I imagine this would be a fairly common use case for RSS feeds, as Wordpress does this out of the box - it generates a feed for every category.

martyf commented

Update to 2.2.9 and should be good to go (you caught me with a spare 20 minutes on my hands)

In your config file, you can add a taxonomies key that accepts the key of the Taxonomy handle, and an array of Term handles. Such as:

'taxonomies' => [
    'tags' => [
        'logic' => 'and', // or could be "or"
        'handles' => [
            'a'
        ]
    ],
    'topics' => [
        'logic' => 'and', // or could be "or"
        'handles' => [
            '1',
            '2'
        ]
    ]
],

This will include entries that have the Tag "a" and the Topic "1" and "2"

Note this is combined with AND logic - so it narrows it down.

You can also use OR logic by changing the "logic" param.

AND means it will get narrower (so must meet ALL conditions). OR means it will be broader.

Note that multiple Taxonomies are always AND - so must have the Tag AND the Topic. But you have the choice on Terms.

How does that sound for you?

Amazing!! I was just starting to custom code this myself haha. I will look at this now.

My custom code was basically following Spatie Laravel feed and having a class map out what's needed:

<?php

namespace App\Feeds;

use Illuminate\Http\Request;
use Spatie\Feed\FeedItem;
use Statamic\Entries\Entry;
use Statamic\Entries\EntryCollection;

class News
{
    public static function getFeedItems(Request $request): EntryCollection
    {
        $request->validate([
            'newsCategories' => [
                'nullable',
                'array',
            ],
        ]);

        // Ensure user enters correct taxonomies
        // Eg "news_category::first-nations-health" is valid
        $newsCategories = $request->input('newsCategories');

        $query = Entry::query()
            ->where('collection', 'news')
            ->where('published', true)
            ->where('date', '<=', now());

        // Filter by news category if set
        if (count($newsCategories) > 0) {
            // Build "OR" taxonomies
            // More information: https://www.martyfriedel.com/blog/filtering-entries-by-taxonomy-in-statamic-3-using-and-or-logic
            $query = $query->whereTaxonomyIn($newsCategories);
        }

        return $query
            ->orderBy('date', 'desc')
            ->orderBy('title')
            ->limit(20)
            ->get()
            ->map(function (Entry $entry) {
                return FeedItem::create([
                    'id' => $entry->url(),
                    'title' => $entry->get('title'),
                    'summary' => $entry->excerpt ?? '',
                    'updated' => $entry->date(),
                    'link' => $entry->url(),
                    'authorName' => '',
                ]);
            });
    }
}
martyf commented

Oh look that's my blog post too ;)

I've got some big plans for Sitemapamic and Feedamic to update their whole codebase, and one thing I want to add is support for easier customising of feeds/maps using a CP interface instead. Which for this instance may be really useful. It's happening but no timeframe as the next few weeks are a tad busy.

Feel free to email me if you have any other ideas or requests though as revamping these is definitely happening.

Let me know how you go (with the update), or if you go your own path too, and if it is all good, I can close this off.

Haha thought you'd like that ;-) your blog has been useful in ramping up my Statamic knowledge from my Laravel knowledge. I'm a blade guy through and through, so my transition has been mostly just getting deep into Statamic itself.

From a feed perspective, having a class that I can customise and utilise the Request input has been great to have the client basically go from /news/?paramhere to /news/feed?paramhere is useful from their perspective without me having to manually set the taxonomy.

Yeah no stress on timeline, it's your app after all! I like that approach. As mentioned above with the class approach, the only downside I can see is you'd have to manually a new feed every time you want a taxonomy to be used. This isn't the worst thing though as it keeps things clean.

martyf commented

I do wonder if that ties in with the CP-level config thought, to allow a feed to be able to accept parameters (for flexibility) or not (for a set structure), and could even include a CP view to help generate a feed URL based on the parameters ticked. Interesting indeed.

I'm glad the blog has been helpful - often when I have those "ooooh I get it now" moments, a post comes out of me as others may benefit too.

I'll close this off, but have made note of the ideas in my to-do list.