Advanced query for all pages with a page-property that has the value of current-page

I would like to create a template that has an embedded query. The embedded query looks for the name of the page the template is on, as the value of properties on other pages. The output is then formatted to show specific properties of those pages and sorted. I should note that I’ve sought help on Discord for this a few times and been given examples of :current-page but never working ones where it’s used to find values in page properties using an advanced query.

Breaking this down:

  1. I need a query that looks for a specified value in the property of other pages - I can do this.
  2. I need a query that specifies the value as the name of the current page - I can’t get this to produce results.
  3. I need to transform the output of the query - I can’t find a good write-up on how this works.

I’ll give a specific example.

I have the template “inventoryitem”. This consists of:

template:: inventoryitem
inventory/type:: 
inventory/title:: 
inventory/author:: 
inventory/actor::

I have the template “catalogue-simplequery”. This consists of:

template:: catalogue-simplequery
{{query (page-property inventory/type <% current page %>) }}

I have a page, “Shadowlands”.
This uses the inventoryitem template and it is the first block of the page.

inventory/type:: DVD
inventory/title:: Shadowlands
inventory/author:: 
inventory/actor:: "Anthony Hopkins"

I have the page DVD. This uses the template “catalogue-simplequery”.
It produces 0 results.

If I manually alter the template to read:
{{query (page-property inventory/type DVD) }}
It produces the output I expect.

Therefore it seemms that <% current page %> isn’t working as a replacement for DVD. I then tried replacing “DVD” with “dvd” in case the query is case sensitive, and it produces no results. I tried keeping the lower case “dvd” in the simple query and renaming the page using the title bar to “dvd” but it still provided no results.

Update:
I’ve found out that changing “DVD” to “dvd” in the “Shadowlands” page allows the <% current page %> query to work. Case sensitivity is clearly not being handled as I’d expect in simple queries (I’d expect everything like this to be totally case insensitve - but it isn’t). This didn’t work to fix the “book” advanced query down below.

I have the page “Call of the Wild”.
This uses the inventoryitem template and it is the first block of the page:

inventory/type:: Book
inventory/title:: Call of the Wild
inventory/author:: Jack London
inventory/actor::

I have the template “catalogue”. This consists of the advanced query:

template:: catalogue
#+BEGIN_QUERY
{
 :query [:find (pull ?p [*])
		:in $ ?current
		:where
			[?p :block/name _]
			[?p :inventory/type ?current]
		]
 :inputs [:current-page]
}
#+END_QUERY

I embed the template “catalogue” on the page “Book”.

What I think this does (but presumably I’m wrong):

  1. pull every block and store as variable p
  2. filter variable p down to every block that has any value for block/name and store as variable p (since only pages have names) - this produces the results I’d expect.
  3. filter variable p to only instances where the property inventory/type has the value ?current (which should equate to “Book”, given it’s on the “Book” page).

This produces no results, instead of the result “Call of the Wild”.

After seeing example queries provided for querying for property values elsewhere, I created this query.

#+BEGIN_QUERY
{
 :query [:find (pull ?p [*])
		:in $ ?current
		:where
			[?p :block/name _]
			[?p :block/properties ?property]
			[(get ?property :inventory/type) ?invtype]
			[(= ?current ?invtype)]
		]
 :inputs [:current-page]
}
#+END_QUERY

Still no results. I’m rather stuck and I’ve been butting my head against this problem with no progress for a while. If anyone can provide a working example of the actual query that will achieve what I want it to, I’d be very grateful.

template:: catalogue-simplequery
{{query (page-property inventory/type <% current page %>) }}

I tested exactly this and when invoking the template it does indeed replace <% current page %> with a reference to my current page ([[My Current Page]]) and the query works as expected.

Maybe you are experiencing a bug?

P.S. do you know that there are also Macros as an alternative to Templates?

Thanks Alex for the response. I wasn’t aware of macros, but the problem for me is more about my lack of understanding than anything else - I want to know why the things I expect to work don’t work, and how to achieve the result with the queries.

As an update, I’ve found out that changing “DVD” to “dvd” in the “Shadowlands” page allows the <% current page %> query to work. Case sensitivity is clearly not being handled as I’d expect in simple queries (I’d expect everything like this to be totally case insensitve - but it isn’t).

I still don’t understand how to write an advanced query to achieve this result.

Bit of a forlorn bump. I’ve tried to mitigate page names by using a property ID, but I can’t work out how to get the ID of the page a query is called from - it all comes back to querying for page names not working as expected. Which is a shame because it’s a super obvious use case.

Indirectly related, this bug Advanced query with `:current-page` on macOS/iOS desktop app return wrong results when switching different pages · Issue #4156 · logseq/logseq · GitHub could be confusing things. It’s certainly confused me for the last couple of hours…

2 Likes

That explains a lot.