惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
宝玉的分享
宝玉的分享
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
V
Visual Studio Blog
Last Week in AI
Last Week in AI
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Microsoft Security Blog
Microsoft Security Blog
D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
P
Proofpoint News Feed
L
LangChain Blog
Blog — PlanetScale
Blog — PlanetScale
The GitHub Blog
The GitHub Blog
博客园 - 【当耐特】
Martin Fowler
Martin Fowler

Example blog

Ghost routing explained with routes.yaml examples — routes, collections & taxonomies Ghost redirects explained with practical redirects.yaml examples How to Serve JSON or XML Data in Ghost How to Change Your Default Post Template in Ghost Ghost Custom Theme Settings How to add Google Fonts to Ghost Themes How to Configure Ghost with Mailgun Ghost Theme Structure & Organization How to Make Custom Templates in Ghost Themes How to Install Ghost CMS via Ghost-CLI How to Add Syntax Highlighting to Ghost The best Ghost CMS hosting platforms Ghost 6.0 — Social Web and Native Ghost Analytics Ghost CMS Social web beta, email 2FA and more author social links How to keep custom design settings when updating your Ghost theme How to use the public preview card in Ghost How to add members-specific sections in Ghost How to customize the default content CTA in Ghost How to build a custom homepage in Ghost An overview of Ghost editor cards and and how they work Ghost CMS News: One-time payments & collecting sales tax How to customize the private site access template in Ghost How to add custom fonts in Ghost themes Ghost CMS News: Additional payment methods & internal linking How to use Code Injection in Ghost How to set meta data of your custom routes & collections in Ghost Ghost CMS News: TK reminders, ActivityPub and improvements How to format dates and use the date helper in Ghost Ghost CMS internationalization, custom fonts & spam protection Understanding and optimizing post and page settings in Ghost CMS
How to Use Ghost Filter Expressions
Bright Themes · 2023-05-19 · via Example blog

Filtering content is an important part of any website, and the filter parameter in Ghost Themes gives us the possibility to fetch content according to tags, authors or other properties depending on the context we are in.

Ghost CMS uses a query language called NQL to create filters for the API results.

Ghost Filter Syntax Reference

Filter Expression

A filter expression is basically a character string that defines logical expressions to be used in filtering records in a specific form.

A filter expression is formed from the following:

  • property - the field on which the filtering will be performed
  • : - separator between property and an operator-value expression
  • operator - this is optional (see below the possible values)
  • value - the value to check against

Property

Properties have to be provided in a certain way, here are the criteria a property must respect:

  • can contain only alpha-numeric characters and _
  • cannot contain whitespace
  • must start with a letter

Other information regarding a property:

  • supports . separated inputs (ex. post.slug )
  • is always lowercase, but accepts and converts uppercase

Value

Can be one of the following

  • null
  • true/false
  • number (integer)
  • literal, character string following these rules:
    • cannot start with -
    • cannot contain any of these symbols: '"+,()><=[] (unless they are escaped)
    • cannot contain whitespace
  • string
    • ' string ' (any character except a single or double quote surrounded by single quotes)
    • single or double quote must be escaped
    • can contain whitespace

Operators

The operators which can be used in 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

In order to combine multiple conditions when filtering you can use the following combinations:

  • + - AND (we get records which respect all conditions separated by +)
  • , - OR (we get records which respect at least one of the conditions separated by , )
  • ( filter expression ) - overrides operator precedence

Ghost Filter Examples

Lets see what we can achieve with filters and some use cases within Ghost Templates.

  1. One of the most common use case is fetching featured posts for your Ghost Blog.
{{!-- Get 3 featured posts --}}
{{#get "posts" filter="featured:true" limit="3"}}
  1. Fetch posts with specific tags to show a list of posts based on the assigned Tag in Ghost Admin.
{{!-- Get 3 posts which have either tech or video tag --}}
{{#get "posts" filter="tags:[tech,video]" limit="3"}}

{{!-- Get 3 posts which have either tech or video tag AND are not featured --}}
{{#get "posts" filter="tags:[tech,video]+featured:false" limit="3"}}

{{!-- Get 3 posts which have primary_tag photo OR has an image --}}
{{#get "posts" filter="primary_tag:photo,image:-null"" limit="3"}}

{{!-- Get 1 post with internal tag red --}}
{{#get "posts" filter="tags:hash-red"" limit="1"}}
  1. Fetch tags and filter by post.count

Fetch all tags, ordered by post count, where the post count is at least 1

{{!-- Fetch 5 tags which are assigned to at least 3 posts --}}
{{#get "tags" filter="post.count:>=3" order="posts.count DESC" limit="5"}}
  1. Fetch authors
{{!-- Get the 3 users with the most posts for tag tech  --}}
{{#get "posts" filter="primary_tag:tech" order="posts.count DESC"  limit="3"}}
  1. Fetch posts and filter by date
{{!-- Fetch 5 posts after a given date }}
{{#get "posts" filter="published_at:>'2020-05-10'" limit="5"}}

Ghost Filter Use Cases

Some more ideas where filters are useful:

  • Creating a related posts for your Ghost Blog, check out this tutorial to see how you can do that.
  • Creating a sidebar section with featured posts
  • Creating a tag cloud
  • Creating a list of authors by tag

Let me know if you have some other use cases in mind, I hope you found this helpful.