Get a list of all pages that link to any task

I’m trying to categorize my TODOs using “topics”. What I usually do is write TODOs with links to other topic pages. Something like:

- TODO Do some task related to [[some-topic]]
- TODO Do some task related to [[some-other-topic]]
- TODO Do some other task related to [[some-topic]]

I would like to be able to query the list of topics that have TODOs associated with them. In this case, it would list “some-topic” and “some-other-topic”. It would also be great if I could then filter what tasks relate to each topic. In this case, clicking in the results on “some-topic” would show me a table containing the two tasks.

I had a look around the forum and other resources and tried some things, but I don’t really know how to start. For example, I don’t know whether I should start by looping over TODOs then trying to figure out what pages are linked to them or whether I should start with pages and see whether they have links to any TODOs.

I would very much appreciate some help with this.

Welcome. I would suggest:

  • A general query to list all pages that they have at least one task related to them:
    #+BEGIN_QUERY
    {:query [:find (pull ?p [*])
       :where
         [?b :block/marker ?m]
         [(= ?m "TODO")]
         [?b :block/refs ?p]
         (not [?p :block/name "todo"])
     ]
    }
    #+END_QUERY
    
  • A parametric query in each such page that lists all tasks related to it:
    #+BEGIN_QUERY
    {:query [:find (pull ?b [*])
       :in $ ?current-name
       :where
         [?current :block/name ?current-name]
         [?b :block/marker ?m]
         [(= ?m "TODO")]
         [?b :block/refs ?current]
     ]
     :inputs [:current-page]
    }
    #+END_QUERY
    
3 Likes