Query won't sort by scheduled date. Filter by current page aliases

  1. How do I sort this query by scheduled date (oldest to newest)?

  2. To show the tasks on the actual current page as well (in addition to blocks with the page ref), what would I need to change?

  3. How can I also include similarly any alias variations of the current page or any of it’s lower namespace levels?

Thanks!

PS- the query is meant to be used in an embed placed on certain pages. Using the “current page” variable I can change the query in its original place and have it update across the board.

#+BEGIN_QUERY
{:title [:b "READY"]
:query [:find (pull ?b [*])
    :in $ ?current-page ?today
    :where
    [?b :block/repeated? true]
    [?p :block/name ?current-page]
    [?b :block/refs ?p]
    [?b :block/scheduled ?d]
    [(<= ?d ?today)]
    (task ?b #{"TODO", "DOING", "LATER", "NOW"})]
:inputs [:current-page :today]}
 :result-transform (fn [result] (sort-by (fn [d] (get d :block/scheduled) ) result))
:collapsed? true
:breadcrumb-show? false
#+END_QUERY
  1. Do you mean to reverse the order of your current sort?
  2. Need to replace [?b :block/refs ?p] with these lines:
    (or
      [?b :block/refs ?p]
      [?b :block/page ?p]
    )
    
  3. Check Advanced Query that pulls all reference AND recursive name spaces
1 Like

I mean sorting results by scheduled date (chronological or reverse is secondary). Right now, results appear sorted by journal page.

Your :result-transform contains a sort-by that reads :block/scheduled . I don’t see any mention of journal in the query.

Exactly. Somehow it’s grouping by journal date and sorting by what looks like journal date. Happens also when the query is directly on the page, as opposed to via an embed (how I’m planning to use it).

Any ideas of why it isn’t sorting by scheduled date? Is this a necessary consequence of the grouping by page?

#+BEGIN_QUERY
{:title [:b "READY"]
:query [:find (pull ?b [*])
    :in $ ?current-page ?today
    :where
    [?b :block/repeated? true]
    [?p :block/name ?current-page]
(or
    [?b :block/refs ?p]
    [?b :block/page ?p]
  )    
    [?b :block/scheduled ?d]
    [(<= ?d ?today)]
    (task ?b #{"TODO", "DOING", "LATER", "NOW"})]
:inputs [:current-page :today]}
 :result-transform (fn [result] (sort-by (fn [d] (get d :block/scheduled) ) result))
:collapsed? true
:breadcrumb-show? false
#+END_QUERY

Current results:

Sorting doesn’t apply in your case, because your query is broken.

  • Should move the closing curly brace }
    • from the end of :inputs [:current-page :today]} ← remove from here
    • to the end of :breadcrumb-show? false ← move it here
  • Also be careful with :collapsed? true , as it tends to misbehave.
    • If that happens, don’t undo/redo, rather create a new block with the correct query.
3 Likes