Query journal blocks that reference a page

I want to query all journal blocks that reference the current page.

#+BEGIN_QUERY
{:title "Diario de clases"
    :query [:find (pull ?h [*])
            :in $ ?current
            :where
            [?h :block/page ?p]
            [?p :page/journal? true]
            [?h :block/refs ?current]]
    :inputs [:current-page]
    :collapsed? false}
#+END_QUERY

This doesn’t get any results, what am I missing? I also tried deleting the “in a journal” constraint and it also doesn’t work

Similar: Query to get a list of pages that refer to the current page?

Should probably replace this:

[?h :block/refs ?current]

with something like this:

[?h :block/refs ?r]
[?r :block/name ?current]
1 Like

It worked, thank you very much!

Final query for those interested:

#+BEGIN_QUERY
{:title [:h2 "Diario de clases"]
    :query [:find (pull ?h [*])
            :in $ ?current
            :where
            [?h :block/page ?p]
            [?p :page/journal? true]
            [?h :block/refs ?r]
            [?r :block/name ?current]]
    :inputs [:current-page]
    :collapsed? false}
#+END_QUERY

But it doesn’t retrieve pages that reference an alias

1 Like

I’m realizing that aliasing is more difficult than I think, in the meantime I just used an OR clause and manually put the alias. Btw, the name MUST be in lowercase!!

Concerning the case, consider also :block/original-name

I tried getting also the aliases, based on:

My query is now this:

#+BEGIN_QUERY
{:title [:h2 "Diario de clases con alias"]
    :query [:find (pull ?b [*])
            :in $ ?current
            :where
            [?b :block/page ?p]
            [?p :page/journal? true]
            [?b :block/refs ?r]
            [?r :block/name ?current]
            (or-join [?b ?r] ; needed because ?a might not unify
              [?b :block/refs ?r]
              (and [?r :block/alias ?a] [?b :block/refs ?a])
            )
    ]
    :inputs [:query-page]
    :collapsed? false}
#+END_QUERY

But it doesn’t return the blocks that use the aliased version

It’s because you still have [?b :block/refs ?r] outside of the or statement.

Oh, I see, because I (accidentally) repeated that clause, it was unified outside the or.

Thank you very much! I love your sample queries, they are a great resource to learn this language.

May I ask what’s the difference between this query and the normal reference section at the end of the page in logseq? Thanks

The references section shows ALL references. This query filters out the ones not in a journal, and using a query allows me to customize the display format.

1 Like