Skip to content

Instantly share code, notes, and snippets.

@hamzamu
Forked from ErisDS/examples.md
Created April 26, 2020 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamzamu/6099fa1a15ceac568dfcfb34ea3d2ade to your computer and use it in GitHub Desktop.
Save hamzamu/6099fa1a15ceac568dfcfb34ea3d2ade 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"}}

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