Dataviewjs query tag and all its subtags

What I’m trying to do

Dataviewjs query tasks matching a particular tag and any nested/subtags—ideally whether or not it has any matching parent tags.
example: if the tag is WANTED,* tags such as #tag/WANTED, #tag/WANTED/subtag, #other-tag/WANTED, or #other-tag/WANTED/subtag, would all be included in the results.

Things I have tried

  • task.tags.contains("WANTED")
  • task.tags.contains("#tag/WANTED")
  • task.tags.includes("#tag/WANTED/")
  • other forms such as icontains, tag.contains, etc. — errors

In context, from a dataviewjs query in my vault:

const tCount = dv.pages("")
    .file.tasks.filter(task => 
          && !task.completed)

I know I have at least one task that matches: incomplete & tagged “#for/:rescue_worker_helmet:/:pill:”, but tCount comes back as 0, so I can’t proceed any further. (In that particular case, I have a tasks plugin callout which is shown or not shown depending on the tCount being 0 or not.)

It doesn’t seem to follow a regular dv query, which does show the subtagged task.

where !completed AND contains(tags, "⛑️")
  • [ ] order medicine #for/⛑️/💊

The DQL query variant of contains() and the DVJS variant of contains() are not the same. The latter checks to see its parameter is an exact match in the array you’re running the function on. (In addition it’ll check agains the “#tag/name” variant, and not just “tag/name”, which also can cause some confusion.)

If you look up the definition of the DVJS variant of contains() it says (from Developer Tools > Console pane):

ƒ (t){return-1!==this.indexOf(t)}

So the easiest way to get the same function as in the DQL query, is to use dv.func.contains(), and then your script should look something like:

const tCount = dv.pages("")
    .file.tasks.filter(task => 
        dv.func.contains(task.tags, "⛑️")
          && !task.completed)

And you’ll most likely get your wanted output.

1 Like

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