How can I list tasks from all notes with a certain tag (using the Tasks plugin)?

I am trying to achieve the following: I would like to list all tasks from all notes with a certain tag (using the Tasks plugin). That is, it’s not about filtering using the tags of the tasks themselves, as in e.g.

    ```tasks
    not done
    tag includes #blocked
    ```

but the tags of the pages in which those tasks live.

Things I have tried

I’ve searched online and here in the forums and couldn’t find anything related. I checked the documentation of the Tasks plugin. I hope I haven’t missed anything.

The docs of Tasks show that you can filter tasks by three different attributes of files (file path, file name, and headings), but not by the tags that a file has.

I’ve dabbled a bit with dataviewjs (e.g., trying to use dv.pagePaths(source)), but that doesn’t work with path includes from Tasks as the latter expects a string or regex and not a data array.

Any ideas on how one could do this? I’m happy to use dataview or dataviewjs if it can’t be done/hacked into Tasks?

Thanks!

Thanks to a hint by ClareMacrae I got it to work (via the Discord).

The key is to use the dataviewjs approach and add the line

${"(path includes " + 
dv.pagePaths(tag).join(') OR (path includes ') + ")"}

where tag is a string value I define as a constant before that line (to make it easier to find it and adapt it to the current case).

A complete example is in the screenshot below.

2 Likes

THANK YOU!!! That’s been bugging me so much (I have a different problem but the fix is the same), and this method is so simple and effective.

1 Like

if you use dataviewjs to run inside a tasks query, any special reason to not do that only with dataview query? For example:

```dataviewjs
dv.taskList(dv.pages('#blocked').file.tasks.where(t => !t.completed))
```

The Tasks plugin provides a bunch of additional task-specific functionality which you don’t get from the Dataview tasklist.

It can be easier to get the list of tasks that you want from Dataview, but then you don’t get the management which the Tasks plugin provides.

2 Likes

So, it’s related with direct edit functionalities provided by Tasks plugin (directly in the query results). Nice to know. Thanks

1 Like

Exactly.

I’ve been able to do this with a dataview query

TASK FROM #agenda 
WHERE !completed

As seen in the above discussion, the point is to still be able to use the further functionalities of the Tasks plugin (i.e., I’d like to avoid re-implementing the Tasks functionalities).

Is there a way to inject a dataview query into a Tasks code block that’s more convenient than the above solution (injecting dataviewjs into a Tasks code block)?

Sorry, I missed that part. I realized when I went back into my task set up I realized my mistake.

Here’s a full version of @jonlemon’s code. My example is the reverse the screenshotted example in that I’m excluding paths not including them.

```dataviewjs
const excluded = '(' + dv.pagePaths('#exclude-global-tasks').array()
  .map(x => 'path does not include ' + x)
  .join(') AND (') + ')'

const query = `
not done
hide task count
${excluded}`

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

To make it more streamlined, I put the base functionality into it’s own script which I call with dv.view():

let excluded = '(' + dv.pagePaths('#exclude-global-tasks')
  .array()
  .map(x => 'path does not include ' + x)
  .join(') AND (') + ')'

dv.paragraph(['```tasks', input, excluded, '```'].join('\n'))

Then anywhere I want to add a Tasks list, I can add the script with the flexibility to change the other Tasks options at whim:

```dataviewjs
dv.view('Utility/Scripts/Dataview/tasks', `
not done
no happens date
priority is above none`)
```
1 Like

Tasks plugin won’t accept this format for a single path (eg using the brackets with only 1 item inside)

@AlanG Is it possible you are using an older version of Tasks?

I fixed it to work with a single-filter inside brackets in 1.14.0.
Here was the original report:

1 Like

Good to know, thanks! I edited my code above to match.

Is there any chance you’ll add a filter to include/exclude pages based on YAML frontmatter? This would negate needing to use the Dataview code at all.

Thanks for updating the post.

Is there any chance you’ll add a filter to include/exclude pages based on YAML frontmatter? This would negate needing to use the Dataview code at all.

Yes, I’m sure it will happen, though I don’t know when.

As you can probably imagine, there is a long list of feature requests, and some bugs and performance issues too. Lots of them are inter-dependent, making it impossible to give timescales.

3 Likes