Query Todos on Current Page

Hello! I’m struggling to figure out how to write a query to get all the todos on a particular page. I thought there might be a generic way to do this for any page, but I’m even struggling to get it to work for a specific page using the page’s name. I think part of it is that I’m just learning the query syntax. Here’s what I’ve tried and learned:

  1. {{query todo}} gives all the todos in my entire database
  2. Then I tried to use a filter by page: {{query todo [[the book]] }} (“the book” is an alias to the page I’m trying to collect todos from). This is giving the same results as #1, which surprised me.
  3. I thought, maybe I need to explicitly use logical expressions to tie the parts of the query together. So I tried {{query (and todo [[the book]]) }}, which was empty.

I’d appreciate any help filling the gaps in my knowledge and creating a query that collects todos on a given page.

5 Likes

Query works for me when To-Do states explicitly specified {{ query (and (todo todo doing) [[the book]]) }}.

1 Like

Thank you @c6p, that worked! Now, any ideas on making this a more general query that will work on any page? I see that for templates there is a “current page” variable, but that doesn’t seem to apply to queries. I’ve also noticed some references to current page in the advanced query syntax, so maybe that’s where I need to be looking…

1 Like

Check out https://logseq.github.io/#/page/dynamic%20variables

{{query (and (todo todo doing) (page <% current page %>))}} grabs To-dos for current page.

10 Likes

Thanks, I missed the “syntax” line on that documentation page when I was looking at it. I appreciate you taking the time to reply and share!

See my feature request for a “page specific NOW section”.

1 Like

Shouldn’t this later reply by c6p be marked the solution @jafish?

2 Likes

Yes, thanks! I didn’t realize you could only mark one solution. I had marked them both, in reverse order, and, well, you saw what happened :wink:

1 Like

This solution does not work for me. I have even copied and pasted your syntax directly in. Weird…

It works for me. Could you make it work? Or what is your output? If you still have problems, you can get quicker responses on discord.

Okay. It’s working! I think it must have been another part of my query which went wrong.

1 Like

Thanks! This works fine on my page (and is another game changer vs. Roam, where I just come from). However, when opening the same page in the sidebar the query gives me the TODOs from the page opened in my main view. Is this intended behavior or a bug?

is possible to target alias with dynamic variable ?
because using <% current page %> , it only get the title of the page

This should be possible with Advanced Queries, using :current-page and :block/alias .

may i ask, if you could help me on that query ?

i have a simple query that is inside a template:
{{query (page-property -zoom <% current page %> )}}

tried this but didnt work:
{{query (page-property -zoom or(:current-page :block/alias) )}}

As said, this needs Advanced Queries. You need to:

  • follow the link
  • do some reading
  • experiment and familiarize with advanced queries
  • come back when you have an advanced query to work with

shure i tried, but didnt get results:

#+BEGIN_QUERY
{
:query [:find (pull ?p [*])
:where
(or
(property ?p :-zoom :current-page)
(property ?p :-zoom :block/alias)
)]}
#+END_QUERY

so in the current page that goes that query , i want to find all the other pages that have the -zoom property that contains the title or alias of the page that goes the query

Here are two options:

  • #+BEGIN_QUERY
    {
     :query [:find (pull ?p [*])
       :in $ ?current-name
       :where
         (or-join [?p ?current-name]
           (property ?p :-zoom ?current-name)
           (and
             [?current :block/name ?current-name]
             [?current :block/alias ?alias]
             [?alias :block/name ?alias-name]
             (property ?p :-zoom ?alias-name)
           )
         )
     ]
     :inputs [:current-page]
    }
    #+END_QUERY
    
  • #+BEGIN_QUERY
    {
     :query [:find (pull ?p [*])
       :in $ ?current-name
       :where
         [?current :block/name ?current-name]
         (or-join [?current ?name]
           [?current :block/name ?name]
           (and
             [?current :block/alias ?alias]
             [?alias :block/name ?name]
           )
         )
         (property ?p :-zoom ?name)
     ]
     :inputs [:current-page]
    }
    #+END_QUERY
    

you are good on this bro.
The second one works, is querying correct , i just have one doubt on the way the information is returning
I see that you use in the query,

[:find (pull ?p [*])

but it is returning me the block instead of the page name, why ? i though the ?p would return me only the page name (that is what im seeking)

  • ?p is defined as a block (or actually something that has properties).
    • It participates in the query as a mere number from the database.
  • To get its name, add [?p :block/name ?p-name]
    • That will also limit the results to pages, as only page-blocks have a name.
  • Then to list that name, replace (pull ?p [*]) with ?p-name