DataView Query for tasks with certain tag

For some time I try to form a query with dataview that gives me all tasks with the tag todo/read.

- [ ] #todo/read https://www.coalition-s.org/resources/rights-retention-strategy/

But I couldn’t get it working and the dataview documentation has very few examples for WHERE <expression>.

TASK FROM #todo/read does not work, because it would also give me pages with the tag #todo/read that have tasks (without this tag; the tag belongs to the page not the task).

My other approach was:

TASK FROM ""
WHERE tasks = "#todo/read"

However, I get absolutely now result here.

What would the correct query look like?

1 Like

Edit: my code was wrong below, read on to the lower posts!

The dataview documentation has a list of field names associated with tasks which I did not see until very recently. It says that task.tags will give you a list of the tags inside the task text (as opposed to file.tags, which has the behavior you describe). So your WHERE clause could look like:

WHERE contains(task.tags, "#todo/read")

(contains is a very useful helper function to see if a list such as task.tags includes some particular item.)

1 Like

I was on that page as well and tried tags, but my mistake was, that I was not aware that I had to prefix it with task.. Thank you!

UPDATED: I was to quick. The following does not work either

TASK FROM ""
WHERE contains(task.tags, "#todo/read")
1 Like

try using tags instead of task.tags:

5 Likes

You were right, you don’t! I apologize for the mistake! @Craig 's solution works for me also (also, I just made sure my dataview is up to date in the Community Plugins settings window while answering another thread).

@Craig or @cutuchiqueno can either of you explain your thought process for why the task. is not necessary here? I was assuming it would be similar to file. but clearly it is not. Thanks!

Hi @scholarInTraining, just to be clear, I don’t have anything to do with Dataview (other than being an avid user).

My best guess is that the WHERE clause always uses the context of the select clause (e.g. TABLE, LIST, TASK). So for LIST, the context is the page itself, and you can access the metadata by using file. For TASK, the context is each individual task, and so you can use the tags field directly. It’s a little confusing because the task also inherits the fields from its parent page, so it’s not always clear what context you’re in.

I hope this helps.

Craig

3 Likes

Thanks @Craig that was exactly the thought process I was looking for from another user of Dataview! :slight_smile: Your explanation makes sense to me too: I forgot about the “page” being different from file but now remembering that dataviewjs does dv.pages() this idea of context makes sense. A very useful concept to learn, thanks again!

As a side note, in the most recent release 5.31 the following was added:

Tasks now have an outlinks list field which includes all links in the task; this can be used for finding tasks with links in them.


I tried it out and was very happy to see it worked. For example, you could do the following query to return all tasks that have a link within their task and also have a #todo tag somewhere within their note.

TASK FROM #todo 
WHERE outlinks

I know this is completely irrelevant in this discussion, but I couldn’t help but mention it because I think it is going to be very useful.

Thanks!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.