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?

2 Likes

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

Hello,

maybe i can use this thread to ask my specific question with a similar topic.

Somewhere in my logseq i write blocks like this. It could be in journal pages or on pages, this shoudn’t matter for the query.

# [[topic1]]
 - TODO make this
 - TODO make that
 - DOING dont forget this and that

# [[otherthings2]]
 - TODO make this
 - TODO make that
 - DOING dont forget this and that

Now i would like to query any TODOs and DOINGs that are parent of a block with pagelink “topic1”.

#+BEGIN_QUERY
{:title [:h3 "ToDos of topic1"]
 :query [
         :find (pull ?b [*])
         :where
         [?p :block/name "topic1"]
         [?b :block/refs ?p]
         (task ?b #{"TODO DOING"})
]
}
#+END_QUERY

The above query finds nothing.
If i remove [?b :block/refs ?p] it finds everything with TODO even [[otherthings2]], even though I specified [?p :block/name "topic1"].

So what is wrong?

Thank!

#+BEGIN_QUERY
{:title [:h3 "Todo by parent block"]
  :query [:find (pull ?b [*])
          :where
          [?t :block/name "topic1"] ;name is always lower-case
          [?b :block/refs ?t] ;get the block that references the page
          [?bl :block/parent ?b] ;get the child block
          [?bl :block/marker ?marker] ;get the marker
          [(= "TODO" ?marker)] ;marker must be TODO
  ]
  :table-view? false ;hides part of query, change to nil to view.
}
#+END_QUERY

Here you go.

2 Likes

brilliant!
Thank you very much! :hearts: