Make Advanced Queries reusable

It would be nice to make advanced queries or their parts reusable in Logseq.

Having that, you don’t need to type them multiple times (adhoc-queries, multiple locations in graph) and can omit redundant query code.

According to, rules are an easy way to do that:

You can abstract away reusable parts of your queries into rules, give them meaningful names and forget about the implementation details, just like you can with functions in your favorite programming language.

So what about adding a additional :query/rules entry to config.edn, similar to existent :query/views and :query/result-transforms for pre-defined :view, :result-transform functions? Then auto-include these to be usable in every advanced query.

Not so well-working alternatives:

  • templates - still redundant, as they are just like copy/pasted text; can get ugly for bigger queries
  • macro/command - didn’t work for me, seems to be not suited well for code incl. format/quoting etc.

Together with Add :pages to :default-queries in config this could make a powerful and flexible query system.

To cover the case «multiple locations in graph» you can embed the block with query. It will work if there is no modifications on query OR if it relies on dynamic variables :query-page / :current-page

Yeah, but would the embedded block see the page it was embedded in as the current page or would it see the original block it was from as the current page?

It seems like that doesn’t quite fit the bill, like for example, for this:

See the demo:
2023-04-21 18.38.42

Thanks for the hint @stdword, this is a good starting point.
But sometimes more flexibility is needed than just dynamic variables :current-page and co. For example to centralize complex query logic in one place or using arbitrary variable values.
My suggestion is to add additional reusable rules to Logseq globally, similar to reusable functions in code.

Well, I totally have a plans to add something about making queries to 🏛 Full House Templates plugin.
Don’t have a vision of how it would look like, but you can subscribe for updates here.

Sharing a collection of query functions here: Advanced Query Collection
Feel free to share your query/transforms and views. They will be added to list.


Aaah, I found this thread searching to see if this was possible, so definitely adding my support for this feature! I have a common clause defining an “active” task in a lot of my queries, and would be much cleaner and less error-prone to abstract it into a single rule:

(or-join [?b ?today]
         (and [?b :block/scheduled ?scheduleddate]
              [(<= ?scheduleddate ?today)])
         (not [?b :block/scheduled _]))

+1 My use case is having the same exclusionary rules across about 8 queries, that’s nearly 8 lines per query and I have to remember to update all 8 queries. I’d love a way to associate a one line rule I can add each time to call on those rules globally.