Follow-up from 2022 post: list tasks from all notes with a certain tag (using the Tasks plugin)

Hi there! I’ve only been using Obsidian for about a week, but after genuine efforts to solve this myself, I’m still getting stuck. Apologies in advance for my lack of knowledge!

What I’m trying to do

Exactly what this poster in 2022 was trying to do: How can I list tasks from all notes with a certain tag (using the Tasks plugin)?

As an example, setting up my GTD system, I’d like to have an ‘inbox’ note, and use it as a brain dump space. I might have three tasks there:

- [ ] Use the beets in something #kitchen
- [ ] Pick up the mail #errands
- [ ] Learn how to use properties in Obsidian

And I’m now trying to make a note like:

---
tags:
  - kitchen
  - someday
  - 🔼
  - project
---
## Kitchen



```dataviewjs
const query = `
not done
${"(path includes " + 
dv.pagePaths(tag).join(') OR (path includes ') + ")"}
# you can add any number of extra Tasks instructions, for example:
group by heading
`;

dv.paragraph('```tasks\n' + query + '\n```');
```

following the instructions (as best I can) from the 2022 post I mentioned. I want it to pull the task about using the beets to the #Kitchen note.

However, I get the following error:

Evaluation Error: ReferenceError: tag is not defined
    at eval (eval at <anonymous> (plugin:dataview), <anonymous>:4:14)
    at DataviewInlineApi.eval (plugin:dataview:18404:16)
    at evalInContext (plugin:dataview:18405:7)
    at asyncEvalInContext (plugin:dataview:18415:32)
    at DataviewJSRenderer.render (plugin:dataview:18436:19)
    at DataviewJSRenderer.onload (plugin:dataview:18020:14)
    at e.load (app://obsidian.md/app.js:1:1147632)
    at DataviewApi.executeJs (plugin:dataview:18954:18)
    at DataviewPlugin.dataviewjs (plugin:dataview:19559:18)
    at eval (plugin:dataview:19478:124)

Things I have tried

Once I first got that error, I then went into both the Kitchen and Inbox notes to define #kitchen as a tag. I also tried defining #kitchen as a tag separately on each of the notes, without it being defined on the other. Same error.

I’ve read through: How to get tasks in current file - Tasks User Guide - Obsidian Publish

I’ve also searched the forums, trying these solutions as well:

I have not tried the AlanG’s solution from the first link, since I’d like to build the query based on the tag itself (i.e. #kitchen), not the lack of all other tags.

I’ve looked through: Properties - Obsidian Help
but clearly I’m missing something, since the defining of the #kitchen tag hasn’t worked yet.

I think I need to at minimum define the #kitchen tag somewhere I haven’t thought of, maybe in a separate .js file in Dataview?

I’ve also tried to look into how to define tags once, globally in my vault, since I’ll also want to create an Errands note and pull all #errands tasks into it, etc. So defining once would be nice. I did find this related feature request:

I’m in the fortunate position of not yet having thousands of notes and hundreds of tags to wrangle or change, so am looking for a way to set this up correctly from the get-go.

Thanks so much for any help you can spare! Obsidian seems really great, and I’ve loved using it recently for the notes from a writing workshop I took. So easy to use for note taking, such clean and easy-on-the-eyes UI. I’m excited to get it set up for task management, too, and never have to look at something like Asana or ClickUp again. :slight_smile:

The source of your problems is this code bit: dv.pagePaths(tag), where tag is undefined within the dataviewjs script. To get to the full list of tags for this file, you need to use dv.current() to access the current file, and then file.etags to get the full lists of tags.

In other words, you need to write dv.pagePaths( dv.current().file.etags.join(" OR ") ) instead of your variant. I’m not quite sure if you want to OR or AND them together. Do you need for all tags of the current document to be present in your result list? or any of them? If all use AND, and if any use OR.


I’m not entirely convinced that this is the best solution to your problem though. Your query would potentially create a very long query for the Tasks plugin to handle, and it might be better to use a simpler query using dataview (within a dataviewjs script). Maybe something like the following:

```dataviewjs

const query = `TASK FROM ${
  dv.current().file.etags.join(" OR ") }
  WHERE !complete `

const result = await dv.query( query )
if ( result.successful ) {
  dv.taskList( result )
 } else
  dv.paragraph("~~~~\n" + result.error + "\n~~~~")
```

I’ve thrown in a little error handling as well. Same caveat regarding the use of all or any of the tags applies to this query too. So you might want to consider having the tags to check for in a separate field somehow, if you don’t want to match against a subset.

Thanks, holroy, for the additional thoughts and assistance. It’s heartwarming to get a reply, and I appreciate your time!

I was able to get the original code I was trying to work without error, as I realized that I thought I’d included the

const tag ="#kitchen"

but actually hadn’t included it. When I do include it, along with the original code from @jonlemon from 2022, it does indeed work. However, it includes all of the tasks from any notes that include the tag #kitchen, not just the tasks themselves that include the #kitchen tag. I’ve played around with trying to exclude the other tags, but run into errors.

I’ve also of course tried the simpler query you suggested, and on the one hand, it’s successful because it doesn’t throw an error, but on the other, it doesn’t pull the #kitchen tasks into the note I’d like them to be filtered to.

I think this part of your code:

const query = `TASK FROM ${
  dv.current().file.etags.join(" OR ") }
  WHERE !complete `

is saying that if the task is from any note/file that includes the tags listed in the current note/file, and the task is not complete, then Dataview should list that task here.

But I haven’t gotten that to do anything. I’ve also tried things like:


const query = `TASK FROM "Inbox" OR ${
  dv.current().file.etags.join(" OR ") }
  WHERE !complete `

since all the tasks I’d like to filter to the Kitchen note that have the #kitchen tag can be found in the Inbox note. That also didn’t work.

I found that when I tried your first suggestion of replacing dv.pagePaths(tag).join(') with dv.pagePaths( dv.current().file.etags.join(" OR ") ) that I get an error about missing a }, though I’ve tried to see where the missing } would be, and best I can tell they all seem accounted for, one opening and the other closing.

I feel like I was getting close to being able to use Obsidian for task management, but I’m feeling like the learning curve is taking long enough that it’s causing me to question whether it’s the right time for me to use it for task management, as opposed to just for notes. Tiny things in the set up throw me off track from just getting going on actually doing a brain dump and getting started. I still have about 4 things that aren’t working, and each causes a stop as I attempt to research it, learn more code (fun! but time consuming! and distracting from my core attempt to get my GTD system set up), and fiddle with possible solutions. And as I get something set up how it would be workable, then I come across another suggestion to fiddle with, try to implement, etc. I might have to save Obsidian for some future date when I don’t have full time work, and have more time to devote to learning the ins and outs of it.

I appreciate you took the time to assist. <3

That’s a very different query, and a lot easier.

Just do:

```dataview 
TASK FROM "Inbox.md" AND #kitchen
WHERE !complete
```

This assumes your Inbox note is in the root of your vault. If not add the folder in front of file name.

I’m sorry for overcomplicating the query, as I thought you knew more of coding, and what a more generic solution.

By the way, if all of your tasks are from that one file, and you always have just one tag, there are also easier ways to do that.

1 Like

Thanks again for the reply, Holroy! I appreciate it.