Query for all tasks referencing a block?

Hi, does exist a way to ask for TODOs referencing (or under) an specific block? Similar as it can be done with page references. I have a task in a block, wich I keep continuing along a week on journals. I would like to have a query with all tasks referencing (or under) that block.
Thanks in advance.

1 Like

If it’s only that specific block, you’ll need to get its ID value (can do with copy block ref) and input that in an advanced query.
Other option is to tag the block and use that specific tag to get it.

I’m sure there’s examples on this forum for this.

Hmm I wonder if there is a way to set up a hierarchy using block references instead of page references and querying any item that references any one block within a chain? Thanks to you, I know how to do that by tagging the blocks but is there any way to avoid that workaround and set up a recursive rule that directly deals with block references (block ID)? I hope that makes sense. I would love to go something like this without using nesting:

TODO Change the sheets
parent-task:: ((TODO Clean your room))

TODO Mop the floor
parent-task:: ((TODO Clean your room))

TODO Clean your room
parent-task:: ((TODO Clean the house))

TODO Clean the house

…and then query for subtasks of «TODO Clean your room», and get ALL of the above, not just that which references the block directly.

Thank you for your help!

You mean a result as such?

Here’s the query for that. It’s actually very simple :slight_smile:
It uses the parent of itself which makes it so easy.

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

Thank you for your response! I’m sorry, I actually mistyped. I meant: how do you query for subtasks of «TODO Clean the house» and get ALL of the above. So it wouldn’t be only looking at the parent of itself but that of the parent too, and so on (until you reach the root). Thank you in advance.

Ah! I should have guessed :slight_smile:
So it’s only slightly more complex, though the query definitely looks more complicated.
Try this:

#+BEGIN_QUERY
{:title "Subtasks"
 :inputs [:parent-block]
 :query [:find (pull ?b [*])
  :in $ ?parent %
  :where
   (get-nested ?b ?parent)
 ]
 :rules [
  [(get-nested ?b ?p)
    [?b :block/refs ?p]
  ]
  [(get-nested ?b ?p)
    [?b :block/refs ?t]
    (get-nested ?t ?p)
  ]
 ]
}
#+END_QUERY

Ps. We could specify we are looking at tasks only if needed. As well as any other limitations you need. They can be added below the line (get-nested ?b ?parent) in the where.

1 Like

Thank you so much! I’ll try it out :pray:

1 Like