Skip to content

Instantly share code, notes, and snippets.

@ErisDS
Last active July 2, 2024 23:52
Show Gist options
  • 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"}}

@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