TODOs from parent and child pages

Hello everyone!

I am using page hierarchy in Logseq, for example, MainPage/Page1. I would like to write a query that retrieves all TODO tasks in MainPage as well as in its sub-pages.

I’m new to writing queries, but I managed to build this query that gets all TODOs in the sub-pages:

#+BEGIN_QUERY
{:title "Child Pages TODOs"
 :query [:find (pull ?b [*])
  :in $ ?current
  :where
   [?c :block/name ?current]     ;; Find the current page
   [?p :block/namespace ?c]      ;; Find the child pages
   [?b :block/page ?p]           ;; Find the blocks in the child pages
   [?b :block/marker "TODO"]     ;; Filter only the TODO blocks
 ]
 :inputs [:query-page]
}
#+END_QUERY

However, I don’t know how to modify it to also retrieve the TODOs that are on the main page.

Any insights or suggestions would be welcome! Thank you!

Welcome.

  • To look into all descendants, consider using (namespace ?page "name")
  • To include tasks in the current page:
    • replace these lines:
      [?p :block/namespace ?c] ;; Find the child pages
      [?b :block/page ?p] ;; Find the blocks in the child pages
      
    • with these ones:
      (or-join [?b ?c]
        [?b :block/page ?c] ;; Find the blocks in the current page
        (and
          [?p :block/namespace ?c] ;; Find the child pages
          [?b :block/page ?p] ;; Find the blocks in the child pages
        )
      )
      
1 Like

Thank you so much !! It works great for the direct descendant pages. I also added the possibility look for blocks referencing the page (sometime I write todos related to the project in my journal when they come to mind).

#+BEGIN_QUERY
{:title "MainPage and Direct Descendants TODOs"
 :query [:find (pull ?b [*])
  :in $ ?current
  :where
   [?c :block/name ?current]     ;; Find the current page
   (or-join [?b ?c]
    [?b :block/page ?c]          ;; Find the blocks in the current page
    (and
      [?p :block/namespace ?c]   ;; Find the child pages
      [?b :block/page ?p]        ;; Find the blocks in the child pages
    )
    ;; Search for blocks referencing #MainPage in any page
    [?b :block/refs ?c]          ;; Find blocks that reference the current page
   )
   [?b :block/marker "TODO"]     ;; Filter only the TODO blocks
 ]
 :inputs [:query-page]
}
#+END_QUERY

However I am not sure that I understand your first comment about using (namespace ?page "name"). It would be useful indeed to be able to look into all descendants, but I don’t understand where I should put this line in the query ?

Try this:

#+BEGIN_QUERY
{:title "MainPage and All Descendants TODOs"
 :query [:find (pull ?b [*])
  :in $ ?current %
  :where
   [?c :block/name ?current]     ;; Find the current page
   (or-join [?b ?c]
    [?b :block/page ?c]          ;; Find the blocks in the current page
    (and
      (p-in-ns ?p ?c)            ;; Find the descendant pages
      [?b :block/page ?p]        ;; Find the blocks in the child pages
    )
    ;; Search for blocks referencing #MainPage in any page
    [?b :block/refs ?c]          ;; Find blocks that reference the current page
   )
   [?b :block/marker "TODO"]     ;; Filter only the TODO blocks
 ]
 :inputs [:query-page]
 :rules [
   [(p-in-ns ?p ?ns)
     [?p :block/namespace ?ns]
   ]
   [(p-in-ns ?p ?ns)
     [?p :block/namespace ?t]
     (p-in-ns ?t ?ns)
   ]
 ]
}
#+END_QUERY