Filtering in Taxonomies

So, I’m trying to find a way to exclude posts with a specific tag from being listed around the site - I want these posts listed only on particular pages. While I thought that I had achieved this with various #get filters in my theme, I have run into a recurring issue with (I thought) pagination. It just repeats the same list of posts, rather than getting the next (25) ones. I looked into it, and found that the actual /page/# page was returning these repeated results.

After a bit of comparing my somewhat modified Casper theme with the default Casper theme, and testing various changes, I found that the error arose when I used the get helper. For instance, in my authors.hbs, I have this:

{{#get "posts" filter="primary_author:{{slug}}+tags:-[hash-secrets,hash-secret,podcast]"}}
    {{#foreach posts}}
        {{> "post-card"}}
    {{/foreach}}
{{/get}}

So the get helper doesn’t support pagination… Fine, and ok. At first, I tried various attempts at using other helpers like ‘has’ but I couldn’t get it working within the foreach iterator (the handlebars documentation more or less says that this won’t work).

Fine again! I’ll just remove the get filter, and use routes.yaml to do the filtering; it’s a cleaner method anyways. This works fine for pages in collections and anywhere that I have things routed via channels, but the problem is that I cannot filter within taxonomies or dynamically channel to taxonomies using {slug}… right? Unlike the use case here, I do not have a clear use case for channels which fixes my problem… at least not without creating a channel for every author and tag page in existence. No problem if it’s just authors, but tags-- well, to my chagrin, the other authors on the site seem to have no restraint for creating new tags.

So, currently, everything is fine except my author and tag taxonomies, which are still using the #get method above and thereby don’t have pagination.

What am I missing? Any suggestions? Maybe I’m just asking too much? If it’s not possible to filter within taxonomies, I’ll likely just remove the whole /tag taxonomy, and create channels for each of my author pages.

Unless there’s some serious privacy consideration you’re worried about the simplest thing to do would be to just make sure {{post_class}} is present within your foreach loops and then use .tag-slug { display: none; } in CSS

Will work globally no problem

Ahh, the old “fix it in CSS” trick. If I were a web dev, I would have thought of that… No privacy concerns with l33t h4ck3rs inspecting the page and finding the links to secrets. It’s all behind authentication anyways.

Ok, this works… after a lot of muddling around.

I tried to just do it globally through the screen.css file, then fix it on pages as needed, but it got a bit tricky. The tag is a class on various objects, especially on posts, and they don’t all have the same display setting (flex, block, etc).

So, instead, I just wrote a little jquery to set the CSS accordingly on pages where I didn’t want it to be seen - in my case, index, author, and tag templates. That’s probably what you were suggesting anyways.

<script>
$(".tag-hash-secret").css("display", "none");
</script>

No more need for the get helpers. I suppose I’ll keep my routes how they are now, and just do this for the author and tags templates.

Thanks!

1 Like

There’s no problem with [whatever display state] is currently set to - you might just be having an issue with specificity. The best way to add this style is to the very bottom of the CSS file, where it will automatically override all other properties (unless they have !important declarations).

Otherwise you can also just use an embedded style tag, which will be significantly more performant than jQuery :slight_smile:

<style>.tag-hash-secret{display:none}</style> 
1 Like

So, putting the css at the bottom of screen.css was the first thing I tried. It had the unintended effect of hiding all posts, post-cards, etc of those tags that I wanted hidden from most of the site. As I mentioned before, I want these posts listed only on particular pages. So for those pages where I wanted the secrets to be shown, I tried to manually fix them, but that didn’t go well. Thus the inverse approach of just hiding it on the remaining non-filtered pages (the taxonomies, since I took care of everything else in routes.yaml).

However, I am more than happy to use the style tag rather than jQuery! duh!

Oh, also I had to fix my post count on my author pages. I did this:

<script>
var num = /\d+/.exec($(".author-stats").text());
num = parseInt(num);
{{#foreach posts}}
	{{#has tag="#secret"}}
		num = --num;
	{{/has}}
{{/foreach}}
$(".author-stats").text(num + " posts");
</script>

It works. That’s all I can say for it.