Query for blocks with a specific parent on the current page

I have a page with a simple structure like this:

I wrote a query to get blocks whose parent is the “Members” block on the current page:

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

This query returns zero results. The way I understand how this query works is:

  • The combination of :inputs [:current-page] and :in $ ?current-page binds the variable ?current-page to whatever :current-page is; I presume it’s the current page.
  • [?b :block/page ?p] constraints ?p to be a page on which blocks in the query (represented by ?b) will be found
  • [?p :block/name ?current-page] constraints ?p to be the current page, which in combination with the previous line, should constrain ?b to be only blocks that can be found on this page.
  • [?b :block/parent ?parent] constraints a new variable, ?parent, to be a parent of blocks represented by ?b
  • [?parent :block/content "Members"] constrains ?parent to be blocks that have the content “Members”. In combination with the previous constraint, this should mean that there is only one: the block containing the content “Members” on this page.

Since ?b is constrained to blocks on ?p, ?p is constrained to be this page, ?parent is constrained to be the “Members” block on this page, and the parent of ?b is constrained to be the “Members” block on this page, then I would expect the output to be the three child blocks under the “Members” block. Where have I gone wrong in understanding how this query works?

1 Like

Hello, I meet the same problem, when I want to count my sub-blocks. I make a simple test like following page :

#+BEGIN_QUERY
{ :title "Current Members"
  :query [:find (pull ?b [*])
          :in $ ?current-page
          :where
          [?b :block/page ?p]
          [?p :block/name ?current-page]
          [?b :block/parent ?parent]
          [?parent :block/content "list2"]]
  :inputs [:current-page] }
#+END_QUERY
- list1
  id:: 6327d682-e63d-48df-aff4-f59814bda490
	- test1
	- test2
	- test3
-
- list2
	- test4
	- test5
	- test6

I use your query, but it just works well when queries test2 which not include block-id data. So I guess when run [?parent :block/content "Members"], the both sides of equation are not same. IDK whether it’s feature or bug, even more how to solve it, last night I take a lot of time to find out some idea but nothing I get.

Now I just combine these blocks with another parent block, like following, cause IDK how to avoid logseq adding data in these blocks, just keep the original😂:

- Expanded Test
  collapsed:: true
query-table:: true
#+BEGIN_QUERY
{ :title "Current Members"
  :query [:find (pull ?b [*])
          :in $ ?current-page
          :where
          [?b :block/page ?p]
          [?p :block/name ?current-page]
          [?b :block/parent ?parent]
          [?parent :block/content "list2"]]
  :inputs [:current-page] }
#+END_QUERY

	- list1
	  id:: 6327d682-e63d-48df-aff4-f59814bda490
		- test1
		- test2
		- test3
	- list2
		- test4
		- test5
		- test6

Hope helpful for you.:grin:

1 Like