Query to list all blocks in current page which are references once or more

How to create a query which lists all blocks in current page which are referenced once or more.

Basically, I want the blocks which have a reference count at the right, the blocks highlighted in below screenshot.

EDIT: This is for blocks which contain one or more references to other blocks:

#+BEGIN_QUERY
{:query [:find (pull ?b [*])
   :in $ ?current
   :where
     [?p :block/name ?current]
     [?b :block/page ?p]
     [?b :block/refs ?refs]
 ]
 :inputs [:current-page]
}
#+END_QUERY

Hi @mentaloid
Is there a way to query all the referenced blocks (only the references) present in a page?
Thanks

Inverting the last condition of the query above, this is for blocks which are referenced by one or more other blocks:

#+BEGIN_QUERY
{:query [:find (pull ?b [*])
   :in $ ?current
   :where
     [?p :block/name ?current]
     [?b :block/page ?p]
     [?other :block/refs ?b]
 ]
 :inputs [:current-page]
}
#+END_QUERY
1 Like

@mentaloid
I tried your query but seems not working

Any idea how to fix it?
My intent is to see in this page only the 2 REF BLOCK 1 & 2 and not the “real” ones.

This is what I’m looking for. Thank you.
The query above this gives blocks which have a block reference, not the blocks which are referenced.

How do I sort this by showing the most referenced at the top?

how to get the ones that are referenced?

@Federico_Frosini, It doesn’t look like your screenshot has references. It has embeds.
But it’s also not clear exactly what issue you’re facing. The screenshot doesn’t show the output. I tried that query and it seems to be working fine for your use case.

Maybe you mean this, which is for blocks that are referenced within the current page:

#+BEGIN_QUERY
{:query [:find (pull ?other [*])
   :in $ ?current
   :where
     [?p :block/name ?current]
     [?b :block/page ?p]
     [?b :block/refs ?other]
 ]
 :inputs [:current-page]
}
#+END_QUERY
1 Like

@mentaloid, can you help me with sorting this query result?
I want the list of blocks to be sorted by the count of references - where the most referenced is at the top

This is more complicated than it sounds (or than it should). If you want to go for it, in How can I calculate how many back links referenced to a particular page? there is a query that:

  • uses a (count ) in :find
  • uses a (sort-by ) in :result-transform
  • renders an extra column in :view
1 Like

Thanks @mentaloid, that’s it.
May i ask you a variation of it: instead of finding the reference block on the current page, how to change to find the reference block inside a parent block?

Something like this:

#+BEGIN_QUERY
{:query [:find (pull ?other [*])
   :in $ ?parent
   :where
     [?parent :block/refs ?other]
 ]
 :inputs [:parent-block]
}
#+END_QUERY