Show all blocks that reference a page using wildcards

Hi,

I’d like to make a query that shows all blocks that reference pages that match a wildcard */improvements

The idea is that I have several projects and open source programs that I use and I store ideas and stuff in the */improvements subpage. I’d like to be able to view all the blocks that reference them

I have a query here that shows all pages using wild cards, but I’m not sure how to combine it with the page-links query to show all blocks that reference the matching pages. Here’s what I have that shows the pages themselves.

#+BEGIN_QUERY
{
:title [:b "Improvements pages"]
:query [:find (pull ?block [*])
:where
[?block :block/name ?pagename]
[(clojure.string/ends-with? ?pagename "/improvements")]
]
}
#+END_QUERY

Here’s my current attempt but it’s not working and I obviously not doing it.

#+BEGIN_QUERY
{
:title [:b "select blocks with links to pages"]
:query [:find (pull ?block [*])
:where
[?block :block/content ?blockcontent]
[?block :block/page ?page]
[?page :block/name ?pagename]
[?block :block/path-refs [:block/name ?pagename]
[(clojure.string/ends-with? ?pagename "/improvements")]
]
]
}
#+END_QUERY

Statement order matters.
We know which pages we want, so let’s start there. This also helps with performance.
Do we want blocks that reference this page, or do we want blocks that are on this page?
Right now your query has both true at the same time.
You state you want blocks that reference this page.
Following this, here’s the query.

#+BEGIN_QUERY
{:title [:b "select blocks with links to pages"]
 :query [:find (pull ?block [*])
  :where
   [?page :block/name ?pagename]
   [(clojure.string/ends-with? ?pagename "/improvements")]
   [?block :block/path-refs ?page]
 ]
}
#+END_QUERY
1 Like

Thanks for the reply! The good news is that this is selecting the correct pages, but it isn’t actually selecting any of the blocks where I’m referencing these pages.

What I do is in my daily notes, I have a section for random quick notes. If I have an idea for an extension or something, I use [[Program Name/Improvement]], and on the next line, I add my idea. This query seems to be display like, an embed of the page, but none of the page-links to the page.

Here’s my page for omnivore improvements:

Here’s what the query is returning:

My original try was copying this query and seeing if I could modify it to use a wildcard for */improvements:

#+BEGIN_QUERY
{
:title [:b "select blocks with links to pages"]
:query [:find (pull ?block [*])
:where
[?block :block/content ?blockcontent]
[?block :block/page ?page]
[?page :block/name ?pagename]
( or
[?block :block/path-refs [:block/name "gardening"]]
[?block :block/path-refs [:block/name "vegetables"]]
)
]
}
#+END_QUERY

Ohhhhh!!! Haha!!!
Took me a minute there. It’s because you are using the alias and not the pagename in the query!

Your pagename is */improvement and not */improvements.

So the query does exactly what you intend. It finds a block that references */improvements as where you define the alias is a block that references a page named */improvements

Remove the s from the query I originally posted.

You will see the alias as in its path-refs is the page, because it is on the page.
You can either filter out blocks on the page. Or change path-refs to refs if you only require blocks that directly reference the page and not where a parent block references it.

To exclude the page:
(not [?block :block/page ?page])

Amazing, thank you! This works perfectly. I’ll share my results below:

#+BEGIN_QUERY
{:title [:b "All blocks related to improvements"]
 :query [:find (pull ?block [*])
  :where
   [?page :block/name ?pagename]
   [(clojure.string/ends-with? ?pagename "/improvement")]
   [?block :block/refs ?page]
 ]
}
#+END_QUERY

To retool for your own purposes you can do the following: The purpose of this is to grab all blocks that reference a subpage in a hierarchy, for example it could grab all references to the [[Work/To Do]] and [[School/To Do]] pages:

#+BEGIN_QUERY
{:title [:b "All blocks related to *"]
 :query [:find (pull ?block [*])
  :where
   [?page :block/name ?pagename]
   [(clojure.string/ends-with? ?pagename "/<subpage_name>")]
   [?block :block/refs ?page]
 ]
}
#+END_QUERY

For this query, just replace “<subpage_name>” with the name of the subpage you’d like to query.

I am using refs instead of page-refs because page-refs was also including aliases, which was adding uncessary bloat to the page.

Thanks again!

1 Like