Advanced query for pages with two tags, or a tag and a status

Hello everyone,

I would like to adapt the Logseq Documents example for querying for pages with a certain tag

{:title "All pages have a *programming* tag"
 :query [:find ?name
       :in $ ?tag
       :where
       [?t :block/name ?tag]
       [?p :block/tags ?t]
       [?p :block/name ?name]]
 :inputs ["programming"]
 :view (fn [result]
       [:div.flex.flex-col
        (for [page result]
          [:a {:href (str "#/page/" page)} (clojure.string/capitalize page)])])}
#+END_QUERY

Instead of having just the programming tag, I would like to refine the results to having 2 tags, “programming” and “priority”

Additionally I would like to know how I can use an advanced query for a page with the tag “programming” and status “ongoing”

Thank you in advance

I have been using the following query to perform a search against two sep tags:

#+BEGIN_QUERY
{:title [:h1 "Example"]
 :inputs ["programming" "priority"]
 :query [:find (pull ?p [*])
         :in $ ?input ?input2
         :where
         [?t :block/name ?input]
         [?p :block/tags ?t]
         [?t2 :block/name ?input2]
         [?p :block/tags ?t2]]}
#+END_QUERY

Thank you, that works perfectly!

1 Like

Is the status also a page-property? As in, in the same block as the tags:: programming with status:: ongoing?
If so then add this:

[?p :block/properties ?prop]
[(get ?prop :status) ?status]
[(= ?status "ongoing")]

Alternatively if you want to add it through the inputs, add an extra input as per the double tags example and use that input instead of "ongoing" (as in: [(= ?status ?input2)] )

no result if I do this …
#+BEGIN_QUERY
{:title “block tags”
:query [
:find (pull ?b [* ] )
:where
[?zp :block/name “tips”]
[?b :block/tags?zp]
]
}
#+END_QUERY

I got all blocks referring to “tips” if I do this …
#+BEGIN_QUERY
{:title “block tags”
:query [
:find (pull ?b [* ] )
:where
[?zp :block/name “tips”]
[?b :block/refs ?zp]
]
}
#+END_QUERY

just don’t know why? thank you in advance !!

[?b :block/tags ?zp] is only available when you use the tags:: property.
[?b :block/refs ?zp] works regardless of how you reference the page in the block as long as Logseq sees it as a reference (i.e. it becomes a link)

Thank you, Siferiax
I’m just a beginner for logseq. Since a block can have any # on itself. Why do we need a tag property on a block? What is it for?

We don’t need it. But to clarify tags:: property isn’t the same as using a hashtag (#).
We can use the tags:: property as a page property to tag that page with certain keywords.
In the same way we can add links to other pages with # or [[ ]] (these are only visually distinct.
When using the property we can indicate the type of relationship (in this case tag) instead of just the link.

In terms of queries :block/refs will always get all references (including if they have been specified using tags::)
:block/tags on the other hand only gets those references that are specified in the tags:: property.
This would be a case where using that tags:: property can be an advantage if you only want a query to pick up on certain references and not others.

Thank you Siferiax. I thought it’s about hash tags. Now it’s clear, just as you said we don’t need it. Thank you again !!

1 Like