Skip to content

Instantly share code, notes, and snippets.

@ErisDS
Last active December 20, 2023 18:05
Show Gist options
  • Star 74 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save ErisDS/f516a859355d515aa6ad to your computer and use it in GitHub Desktop.
Save ErisDS/f516a859355d515aa6ad to your computer and use it in GitHub Desktop.
Ghost Filter Query examples

Filter Queries - Example Use Cases

Here are a few example use cases, these use cases combine filter with other parameters to make useful API queries. The syntax for any of this may change between now, implementation, and release - they're meant as illustrative examples :)

Fetch 3 posts with tags which match 'photo' or 'video' and aren't the post with id 5.

api.posts.browse({filter: "tags:[photo, video] + id:-5", limit="3"});

GET /api/posts?filter=tags%3A%5Bphoto%2Cvideo%5D%2Bid%3A-5&limit=3

{{#get "posts" filter="tags:[photo,video]+id:-5" limit="3"}}

Fetch posts which have either a tag of 'photo', are marked 'featured' or have an image

api.posts.browse({filter: "tag:photo,featured:true,image:-null"})

GET /api/posts?filter=tag%3Aphoto%2Cfeatured%3Atrue%2Cimage%3A-null

{{#get "posts" filter="tag:photo,featured:true,image:-null"}}

Fetch all tags, ordered by post count, where the post count is at least 1

api.tags.browse({filter: "post.count:>=1", order: "posts.count DESC", limit: "all"})

GET /api/tags?filter=post.count:>=1&order=posts.count%20DESC&limit=all

{{#get "tags" filter="post.count:>=1" order="posts.count DESC" limit="all"}}

Fetch posts by the author 'hannah' which are marked as featured

api.posts.browse({filter: "author:hannah+featured:true"});

GET /api/posts?filter=author:hannah%2Bfeatured:true

{{#get "posts" filter="author:hannah+featured:true"}}

Fetch the 3 most prolific users who write posts with the tag 'photo' ordered by most posts

api.users.browse({filter: "posts.tags:photo", order: "posts.count DESC", limit: 3});

GET /api/users?filter=posts.tags:photo&order=posts.count%20DESC&limit=3

{{#get "users" filter="posts.tags:photo" order="posts.count DESC" limit="3"}}

Fetch 5 posts after a given date

api.posts.browse({filter: "published_at:>'2015-07-20'", limit: 5});

GET /api/posts?filter=published_at:%3E'2015-07-20'&limit=5

{{#get "posts" filter="published_at:>'2015-07-20'" limit="5"}}

@0xAl3xH
Copy link

0xAl3xH commented Aug 12, 2016

I am interested in the above question as well.

@helloncanella
Copy link

helloncanella commented Sep 2, 2016

How to use the value of variable as a string in the filter.

I'm trying to filter all posts with the tag value given by {{tags from="2"}}, but I can't.

@oscarmorrison
Copy link

Hey Hannah @ErisDS is there any way to filter substring.

Eg. I want to have a navigation with only some tags.

E.g tags that have the description with tag #features

@skxctech
Copy link

I'm interested in also filtering out a tag, which I can't seem to figure out.

Tried with:

tags: -tag
tags: [-tag]
tags: -[tag]
tags: [tag1, tag2, -tag3]

No clue what to do so far, or if this is possible.

@Kogenesis
Copy link

Kogenesis commented Oct 24, 2017

A workaround to use posts that have both tag one AND two

{{#get "posts" filter="tags:one" include="tags"}}
	{{#foreach posts}}
		{{#has tag="two"}}
			<div class="recent-title"><a href="{{url}}">{{title}}</a></div>
		{{/has}}
	{{/foreach}}
{{/get}}

Also, to filter out a tag, just use ^has for hasn't

@fngrz
Copy link

fngrz commented Nov 18, 2018

any ideas for pulling a random post?

@daniel-moya
Copy link

any ideas for pulling a random post?

Just pull X number of posts with determined Limit=m

@luizamboni
Copy link

Great

@iftkharhussain
Copy link

Hi, can anyone Please tell me how can i get all posts have hash tag (internal tag or private tag). Actually i did not find custom post type in Ghost that's why i ma filtering posts having specific has tag post. Is there any better way to do this. ?

@ralyodio
Copy link

ralyodio commented Apr 4, 2020

how do i get random 5 tags?

@mlicheng
Copy link

good job

@csellis
Copy link

csellis commented Oct 4, 2020

Is there a way to fetch 3 posts that do not match certain tags?

Fetch 3 posts with tags which match 'photo' or 'video' and aren't the post with id 5.
api.posts.browse({filter: "tags:[photo, video] + id:-5", limit="3"});

GET /api/posts?filter=tags%3A%5Bphoto%2Cvideo%5D%2Bid%3A-5&limit=3

{{#get "posts" filter="tags:[photo,video]+id:-5" limit="3"}}

@luizjr8
Copy link

luizjr8 commented Oct 9, 2020

To filter posts with more than one tag (AND):

E.g: unama AND ead

{filter: "tags:unama+tags:ead"}
&filter=tags%3Aunama%2Btags%3Aead

Dont forget to escape the string value.

@malikmukhtar
Copy link

@luizjr8 it works perfectly

@84896150
Copy link

84896150 commented Jul 5, 2022

api.tags.browse({filter: "post.count:>=1", order: "posts.count DESC", limit: "all"})
No data is available,filter: "post.count:>=1" ,There is a problem

@cathysarisky
Copy link

I'm interested in also filtering out a tag, which I can't seem to figure out.

Tried with:

tags: -tag tags: [-tag] tags: -[tag] tags: [tag1, tag2, -tag3]

No clue what to do so far, or if this is possible.

Very late to the party, but I stumbled here while looking for my answer, so: To include some tags and exclude others, do:
tags:[include,these,tags]+tags:-[skipme]

@chof64
Copy link

chof64 commented Jan 18, 2023

This is about filtering by internal tags, tags that's starting with a hash (#). Add the hash- prefix to the tag name. For example, your internal tag is #special. Use tags:[hash-special] as your query.

@ramrami
Copy link

ramrami commented Sep 6, 2023

Anyone was successful in filtering tags by post count? Nothing seems to work.

@etothepowerofitimespiequalsminusone

For reference to myself. Filter for posts that are published on a specific day the following syntax allows 2 dates:
'published_at:>2023-10-04+published_at:<2023-10-05'

You could probably use the same for other date fields.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment