Is it possible, with Macros, to run a db query for a block-id an build the {{embed ((block-id))}} inside a page?

Hi guys, I was wondering if Macros can be used for the following scenario:

  1. search for and find an uniquely-distinguishable block and retrieve it’s Block-ID (using a database query from the macro itself?);
  2. build the {{embed ((block-id))}}

I would like to tell the Macro {{{embed “uniquely-distinguishable text in Block or property” /today }}} and get as a result a block with en embed just like I would do with Ctrl+E/Ctrl+V

{{embed ((64f02fde-f8a4-4c6b-bdfe-3e5acdc9ef36))}}

Macros don’t support Advanced Queries:

And you need Advanced Queries to retrieve a block’s ID.

But you could take another route and use a Simple Query:

{{query (and “search terms here” (sample 1))}}

It should be possible to define a macro with that and style it with CSS to make the result look like an embedded block.

I actually need to be able to click inside the embed and do my edits to that block …
I have tested the following:

  1. create macro in config.edn: "get-embed" "{{embed $1}}"
  2. call the macro with: {{{get-embed ((64f02fde-f8a4-4c6b-bdfe-3e5acdc9ef36))}}}


Now I know I can get the Block-ID from a Complex Query but how to pass it as an argument to a macro?

I didn’t find much help online on Logseq Macros and I am not a developer so my understanding would be limited anyway but I thought that I can define a complex query with a function or some name in config.edn and call it from the macro directly…

Maybe it is possible to write a closure function in config.etn that runs the query and then the macro to just call that function …

Isn’t this the case for a query result? What’s stopping you from trying the approach I suggested?

I didn’t quite get what you were proposing: so I define a macro in which I write the simple query in which I search for some search terms but then I got confuse what is (sample 1)…

I actually stumbled upon the github issue about complex queries not able to be run in macros cause that was actually what I was looking for.

You said you want “uniquely-distinguishable text in Block or property” as the input to find a block.

The simple query to do so is a full-text search one:

{{query (and “search terms here” (sample 1))}}

(sample 1) just means “take only the first result”.

From there define the macro.

My situation requires a complex query because I need to filter the found block for children and get the block-id of the last immediate child of said parent block.

With a general situation your solution should work, at least the non-CSS part.

So what the macro will look like in your intention?

{{get-embed what?}}

Maybe you have an Advanced Query and you want to reuse it by changing something in it?

So i basically want to perform a complex query that would return the last direct child of a parent-block with distinct name and use that block-id in an embed created by a macro :crazy_face:

I have solved, in other thread, the query issue (thanks @Siferiax ) but I still wanted to reuse the code for other sections of my Daily Journal where i usually insert new notes so I can quickly capture straight to those sections.

So, I would call, in the Contents Page in the Right Sidebar:
{{get-embed "### Journal Entries" <%today%>}}
{{get-embed "### Today's Metrics" <%today%>}}

This was my idea.
So I thought a Macro can call a query saved in config.edn and use the parameters $1, $2 to tailor the query, then create the string:

{{embed ((64f02fde-f8a4-4c6b-bdfe-3e5acdc9ef36))}}

Apparently that is not possible…

Yeah, would be very cool if something like this is possible.
Invoking queries defined in the config file in general. I think lots of people may like that.

I don’t know how many cases other than “Journal Entries” and “Today Metrics” you have, but have you thought about:

  1. having a query for each of them

  2. copy the block ID of each query

  3. create for each of them a macro like

    "journal-entries" "{{embed ((12345-6789-...))}}


Just an idea, I have not tried.

I just find it to be an elegant solution if the macro can call a query or a function and get a result to a block. I am, of course, doing it differently atm but I try to optimize, reduce steps, future-proof, etc.

I am currently using tabs I leave opened each morning and switch to when I want to input a note into a specific section because, when zoomed in, you get that last line where you click and you get a new bullet, instead of Ctrl_Clicking the previous Block, then press Enter Twice to achieve the same.

Again, it was an exploration of how to do things more elegantly and reduce steps between idea and noted info.

I wasn’t expecting that you can’t run complex queries from macros, because simple ones you can…

In Generate explicit hierarchy out of properties:

  • advanced queries are defined as simple strings
  • macros accept parameters
  • the queries are executed passing those parameters as inputs

If we come with a nice generic design, all the pieces are there, it only remains to connect them (e.g. the config file is too heavy, queries don’t belong there). If you are interested, I can open a separate thread, and leave this one for the embedding part.

1 Like

Complex Queries in Macros is not possible so this thread is past its prime. Feel free to use this or open new thread. I agree config.edn for everything is not a good solution. I even think Custom Commands should have a separate file.


Seems that, by:

  • leaving an empty line
  • escaping double quotes

…it is already possible to:

  • define an advanced query in a macro:
      :title \"$1\"
      :query [:find (pull ?p [*])
        :where [?p :block/name \"$2\"]
  • use it with parameters: {{macroname title, pagename}}

@mentaloid I tried the above approach in a macro that uses the kits js code to replace itself with its result and indeed it replaces the {{maroName}} with something, but not the result of the Query -unfortunately- but the Query itself … quite obvious i’d say now.

For my personal request, it would be immensely useful if I can get the result of the query (say, the parent block’s Ref-ID) as text as the output of the Macro, not the query itself…

Not sure that’s possible with the current implementation of kits but maybe such an option wouldn’t be far off?

You are essentially asking for multiple steps to take place at once:

  • Execute a first macro
    • to form a query
  • Execute the formed query
    • to acquire its result
  • Form a second macro with the acquired result
    • and execute that too

Mind that every next step depends on (and thus has to wait for) the result of the previous step. Both the specific kit and the macros (as far as I know) assume a single execution, there is no automatic chaining from one execution to another (not even addressing the need to pipe the previous output as next input). Therefore, you need specialized code (e.g. a dedicated kit) to perform all the steps in a single go.