How to filter all tasks with tag MYTAG or page tag MYTAG

Hi, I am using tag MYTAG in my journals to create my tasks list every day:

#MYTAG
    TODO task1
    TODO task2

I also have notes from meetings and I am using MYTAG as page-tags. Now, I would like to have all todos at one place so I am trying to create query that will filter it for me. I used to filter just by page tag

{{query (and (task  TODO NOW LATER DOING) [[MYTAG]] )}}

This works fine, except I find out, it not filter tasks from my meetings, where I am using page-tags. I would like to filter all TODOs where is tag MYTAG or page-tag MYTAG. I am trying this:

{{query (and (task  TODO NOW LATER DOING) (or [[MYTAG]] (page-tags MYTAG)) )}}

But this results zero tasks. The logic looks OK with the or statement but I am not sure if this is some limitation of logseq queries or where could be a problem. I also tried Advanced queries but it looks quiet complicated to understand how to implement basically any query.

How can I filter my TODO tasks properly with simple query and what am I doing wrong?

1 Like

How about:

{{query (and (todo todo now later doing) [[MYTAG]] )}}

Hi, thank you for your response. As I wrote in the description, it works fine when queriing only for tags or page tags but not together with or condition. So tasks with all states upper case or lower case, does not matter. That is not an issue.

Please read the syntax properly

Sorry I missed that there is small diff. I just tried it and the result is the same like with my query:

{{query (and (task  TODO NOW LATER DOING) [[MYTAG]] )}}
1 Like

todo is an obsolete way to query tasks. task is task-workflow-independent.



@Dorinand, try this one:

#+BEGIN_QUERY
 {:title "`or-join` example"
  :query [:find (pull ?b [*])
          :where
          (or-join [?b ?n]
                 (and [?b :block/page ?p] [?p :block/tags ?t] [?t :block/name ?n])
                 (and [?b :block/path-refs ?r] [?r :block/name ?n])
          )
          [(= ?n "mytag")]
          (task ?b #{"NOW" "LATER" "DOING" "TODO"})
     ]
  :collapsed? false}
#+END_QUERY

May be someone could provide a more simple query.

Hi, thank you but this does not work at all, result is zero again. Is there any good tutorial for understanding this queries? For me it looks quiet hard to understand how does it work. Of course, there is some examples in doc, but nothing more.

@stdword’s query is working for me. Maybe you can also try this one,

#+BEGIN_QUERY
{:title "All tasks tagged with MYTAG"
 :query [:find (pull ?b [*])
         :where
         (task ?b #{"NOW" "LATER" "DOING" "TODO"})
         [?b :block/page ?p]
         [?r :block/name "mytag"]
         (or [?b :block/path-refs ?r]
             [?p :block/tags ?r])]}
#+END_QUERY

The resources section in the documentation is more than enough. You can also refer to this tutorial in addition to them.

2 Likes

@starryveshch this query does not work, number of results is zero.

I have to say that from the documentation of logseq is quiet hard to understand syntax. Thank you for tutorial, it looks good. I am missing knowledge of where section, what does question mark with character at the beginning and end of line means.

I test this query

#+BEGIN_QUERY
{:title "Find: TODO MyTag"
:query [:find (pull ?b [*])
	:where
	[?b :block/marker "TODO"]
	[?p :block/name "MYTAG"]
	[?b :block/ref-pages ?p]]
	}
#+END_QUERY

It works but only if I set block with tag on the same journal - tested on today date. Other TODOs with tag does not work… Also, when I test it with testing graph, I find out tag name is case sensitive.

The query is completely correct:

If you still got zero results try to restart application: there are some problems with queries caching since last app update.

And note: tags are case insensitive, BUT it is important to use lowercase block names in advanced queries (because of tech reasons)

1 Like

Thanks for this simple version :pray: — it also works correct!

Thanks for correcting me. Didn’t notice the change.

My apologies.

That’s where the issue is; the :block/name stores the page name in lower case, i.e., MYTAG is stored as mytag. So if you want to use the exact page name in the query, then replace the :block/name with :block/original-name.

#+BEGIN_QUERY
{:title "All tasks tagged with MYTAG"
 :query [:find (pull ?b [*])
         :where
         (task ?b #{"NOW" "LATER" "DOING" "TODO"})
         [?b :block/page ?p]
         [?r :block/original-name "MYTAG"]
         (or [?b :block/path-refs ?r]
             [?p :block/tags ?r])]}
#+END_QUERY
1 Like

I upgraded to latest version 0.7.0 and queries started working, the problem was probably with the version of logseq I did not mention here.

Also this

@starryveshch could you explain how does the query is processed? I understand that ?p, ?b and ?r are variables, but even after reading some tutorial, I am not able to understand the logic of queries. Can you describe step by step what each line do, where it stores and how is it passed to the next clause? What if I want to filter based on multiple tags not just MYTAG but also MYTAG2? Thank you.

Could this queries make logseq slow? It is extreamly slow since I jump to version 0.7.0, response time on click is basically in seconds. Also, I find out, some TODO is not filtered by the query. This is in my todays journal:

#aaa
- #migration #othertag
  - TODO Task one
  - TODO Task two

Interesting is, task one is filtered but task two does not. But when I moved task two on other page:

- tags:: migration
- TODO Task two

it somehow works… I am quiet lost how does this even works. I do not see there any logic what is filtered and what is not and why.

Currently running logseq version 0.7.0

Hi Dorinand,
First: Logseq is slow. Because it uses files instead of a database. At first it is nice because of the text files but the more files you have the slower it gets. This is why i am really thinking about it whether this will be my further way or not.
To get around with your tag - i wouldn’t setup the query with the tag. I would choose the clojure string to catch them:

#+BEGIN_QUERY
{:query [:find (pull ?b [*])
:where
[?b :block/marker ?marker]
[(contains? #{"TODO" "NOW" "LATER" "DOING" "WAITING"} ?marker)]
[?b :block/content ?c]
[(clojure.string/includes? ?c "#myTAG")]
]
:result-transform (fn [result]
         (sort-by (fn [h]
         (get h :block/priority "Z")) result))
:breadcrumb-show? false
}
#+END_QUERY

Of course you can play around and for further filtering you can add another string like this:

#+BEGIN_QUERY
{:query [:find (pull ?b [*])
:where
[?b :block/marker ?marker]
[(contains? #{"TODO" "NOW" "LATER" "DOING" "WAITING"} ?marker)]
[?b :block/content ?c]
[(clojure.string/includes? ?c "myTAG")]
[(clojure.string/includes? ?c "anotherTAG")]
]
:result-transform (fn [result]
         (sort-by (fn [h]
         (get h :block/priority "Z")) result))
:breadcrumb-show? false
}
#+END_QUERY

So play around with #myTAG or only myTAG or anotherTAG and so on and it will pull them.
All the above methods will not work if you do inconsequent tagging like this: myTAG or #myTAG or [[myTAG]] or #[[myTAG]] or [[#myTag]] (…exaggeration…).
But they all get catched with clojure string.

1 Like

Hi Yupp1018, thanks for the closure string technique.
However, was trying to use your multi tag closure string code and couldn’t get it to work for a use case scenario as below
[(clojure.string/includes? ?c "Company)]
[(clojure.string/includes? ?c “company”)]
to make query case insensitive, but no results show up.
If only one of the above is used it works but not when combined. Would you be knowing why this is, I’m using 0.8.7 version
Also the [h] what does that refer to?