Skip to content

Instantly share code, notes, and snippets.

@biapar
Forked from ErisDS/examples.md
Created September 9, 2020 13:54
Show Gist options
  • Save biapar/d129c6bacc467c0bb2a8fd16a19915e1 to your computer and use it in GitHub Desktop.
Save biapar/d129c6bacc467c0bb2a8fd16a19915e1 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