Filtering content is a significant factor of every website, and the filter parameter in Ghost themes allows us to get information based on tags, authors, or other features depending on the context by using Ghost filter expressions.
To generate filters for API results, Ghost CMS use a query language known as NQL.
Content API Client in Ghost
Ghost includes a flexible promise-based JavaScript framework for interacting with the Content API. The library may be used in any JavaScript project, client or server side, and abstracts away all of the annoyances associated with working with API data.
Ghost Filter Expression
A filter expression is a character string that provides logical expressions that will be used to filter records in a specific format.
A filter expression is made up of the following elements:
- property – the field where the filtering will take place
- : – separator between a property and a value expression
- This is an optional field (see below the possible values)
- value – the value to be compared against
Properties
Properties must be given in a specific manner; below are the requirements that a property must meet:
- may only include alpha-numeric characters and _
- cannot include whitespace
- must begin with a letter
Value
It might be one of the following:
- null
- true/false
- number (integer)
- literal
string
Operators
The operators which can be used in Ghost filter expressions:
- – – negation
- > – greater than
- >= – greater than or equals
- < – less than
- <= – less than or equals
[ value1, value2, … ] – match any value in the list of values ( can also be used in combinations with – to negate )
Combinations
When filtering, you may use the following combinations to combine several conditions:
- + – AND (we obtain records that satisfy all conditions separated by +)
- , – OR (we obtain records that satisfy at least one of the criteria separated by,)
- (filter expression) – takes priority above operator precedence
Examples of Ghost Filters Expressions
Let’s examine what we can do with filters and use cases with Ghost Themes.
Fetch 3 posts with tags which match ‘photo’ or ‘video’ and aren’t the post with id 6.
api.posts.browse({filter: "tags:[photo, video] + id:-6", limit="3"});
GET /api/posts?filter=tags%3A%6Bphoto%2Cvideo%6D%2Bid%3A-6&limit=3
{{#get "posts" filter="tags:[photo,video]+id:-6" limit="3"}}
Fetch posts by the author ‘themeix’ which are marked as featured
api.posts.browse({filter: "author:themeix+featured:true"});
GET /api/posts?filter=author:themeix%2B
featured:true
{{#get "posts" filter="author:themeix+featured:true"}}
Fetch posts and filter by 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"}}
Use Cases for Ghost Filter Expressions
Here are some additional applications for ghost filter expressions:
- Check out this guide to learn how to make related posts for your Ghost Blog.
- Creating a highlighted post area on the sidebar
- Developing a Tag Cloud
- Making a tag-based author list
Sometimes we need to use a filter to find our desired search in ghost themes. After reading this post I think you easily add Ghost filter expressions to your ghost website. Besides this, we are a ghost developer agency. We developed fantastic ghost themes as your requirements, if you need custom ghost themes you can Contact us or visit our theme gallery.
No Comment