Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"}}

@johnthillaye

This comment has been minimized.

Copy link

@johnthillaye johnthillaye commented Jan 21, 2016

How would you filter to have posts with both tags video and photo ?

@devingfx

This comment has been minimized.

Copy link

@devingfx devingfx commented May 16, 2016

Hello,
I did'nt get to filter several tags neither...
What I tried, no one seems to work:

tags:one+two
tags:one+tags:two
tags.slug:one+tags.slug:two
tags:[one+two]
tags.slug:[one+two]
tags:(one+two)

How do you get one post that has X tag on it?

@al3x-huang

This comment has been minimized.

Copy link

@al3x-huang al3x-huang commented Aug 12, 2016

I am interested in the above question as well.

@helloncanella

This comment has been minimized.

Copy link

@helloncanella 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

This comment has been minimized.

Copy link

@oscarmorrison oscarmorrison commented Jan 8, 2017

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

This comment has been minimized.

Copy link

@skxctech skxctech commented Aug 10, 2017

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

This comment has been minimized.

Copy link

@Kogenesis 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

This comment has been minimized.

Copy link

@fngrz fngrz commented Nov 18, 2018

any ideas for pulling a random post?

@daniel-moya

This comment has been minimized.

Copy link

@daniel-moya daniel-moya commented Oct 15, 2019

any ideas for pulling a random post?

Just pull X number of posts with determined Limit=m

@luizamboni

This comment has been minimized.

Copy link

@luizamboni luizamboni commented Dec 7, 2019

Great

@iftkharhussain

This comment has been minimized.

Copy link

@iftkharhussain iftkharhussain commented Dec 23, 2019

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. ?

@chovy

This comment has been minimized.

Copy link

@chovy chovy commented Apr 4, 2020

how do i get random 5 tags?

@mlicheng

This comment has been minimized.

Copy link

@mlicheng mlicheng commented Aug 15, 2020

good job

@csellis

This comment has been minimized.

Copy link

@csellis 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

This comment has been minimized.

Copy link

@luizjr8 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.

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