Exclude Tasks from a certain pages

Apologies if you’ve seen this question before. I was doing reasonably okay with queries until I get this problem.

I have a “catch-all tasks” query, but its grabbing tasks I have placed in my templates. Not ideal.

Is there a way to exclude all tasks which have a certain tag, or linked to a page? I found the “not=” operator in datalog docs but it doesnt seem straight forward.

Any suggestions appreciated.

replace “tag” with your tag/link that you want to exclude. use lower-cased text.

#+BEGIN_QUERY
{
 :query [:find (pull ?b [*])
         :where
         (task ?b #{"TODO"})
         [?ref :block/name "tag"]
         (not [?b :block/refs ?ref])
       ]
}
#+END_QUERY
2 Likes

@Darwis thanks for your solution! However, I’m struggling to add prioritization of tasks to this query. I am totally new to this language and syntax, I’m wondering if you (or anyone else) might be able to help make the following query work:

#+BEGIN_QUERY
{
 :query [:find (pull ?b [*])
:result-transform (fn [result]
                                 (sort-by (fn [h]
                                                  (get h :block/priority "Z")) result))
         :where
         (task ?b #{"LATER"})
         [?ref :block/name "habit"]
         (not [?b :block/refs ?ref])
       ]
}
#+END_QUERY

Solved this myself, in case this is helpful for anyone else. The :result-transform section comes at the end and is outside of the last square bracket closure as follows:

#+BEGIN_QUERY
{
 :query [:find (pull ?b [*])
         :where
         (task ?b #{"LATER"})
         [?ref :block/name "habit"]
         (not [?b :block/refs ?ref])]
:result-transform (fn [result]
                             (sort-by (fn [h]
                             (get h :block/priority "Z")) result))
}
#+END_QUERY

And showing some blocks with particular tag but excluding the ones that are a TODO DOING DONE?

The goal is to show just the ones in the [[backlog]] that are still not active (backlog).