Tutorial: Find all Pages linked (recursively or direct) (Advanced Query)

(RECURSIVE) All pages the current refers to and so on

#+BEGIN_QUERY
{:query [:find ?tag
   :in $ ?current %
   :where
     [?p :block/name ?current]
     (pr ?p ?tag)
   ]
 :inputs [:current-page]
 :result-transform (fn [result] (sort result))
 :view (fn [tags] [:div (for [tag (flatten tags)] [:a.tag.mr-1 {:href (str "#/page/" tag)} (str "[[" tag "]] ")] )] )
 :rules [
   [(pr ?p ?tag)
     [?b :block/page ?p]
     [?b :block/refs ?ref]
     [?ref :block/name ?tag]
   ]
   [(pr ?p ?tag)
     [?b :block/page ?p]
     [?b :block/refs ?ref]
     (pr ?ref ?tag)
   ]
 ]
}
#+END_QUERY

Pages to which the current one refers directly

#+BEGIN_QUERY
{:query [:find ?tag
   :in $ ?current
   :where
     [?p :block/name ?current]
     [?b :block/page ?p]
     [?b :block/refs ?ref]
     [?ref :block/name ?tag]
 ]
 :inputs [:current-page]
 :result-transform (fn [result] (sort result))
 :view (fn [tags] [:div (for [tag (flatten tags)] [:a.tag.mr-1 {:href (str "#/page/" tag)} (str "[[" tag "]] ")] )] )
}
#+END_QUERY

Pages that refer to the current directly

#+BEGIN_QUERY
{:query [:find ?tag
   :in $ ?current
   :where
     [?p :block/name ?current]
     [?b :block/refs ?p]
     [?b :block/page ?page]
     [?page :block/name ?tag]
 ]
 :inputs [:current-page]
 :result-transform (fn [result] (sort result))
 :view (fn [tags] [:div (for [tag (flatten tags)] [:a.tag.mr-1 {:href (str "#/page/" tag)} (str "[[" tag "]] ")] )] )
}
#+END_QUERY

I really like this query — it perfectly fits my purpose.

Additionally, I’d like it to exclude and only hide pages that have the exclude-from-graph-view property, but I’m not exactly sure how to do that.

I don’t know much about queries, so I tried using AI to figure it out, but all my attempts failed.

If anyone could offer some help, I’d really appreciate it.

Something like this:

[?p :block/properties ?props]
[(get ?props :exclude-from-graph-view)]

I’m using the recursive query from “short,” but I modified the part in the view function from (str "[[" tag "]]") to (str "" tag ""), and everything else is the same.

I manage my page categories with #tags, but I don’t want those simple category references created by #tags to appear in the graph view. So, I added the exclude-from-graph-view property to all #tags pages.

Now, I want to apply the same rule in the recursive query — I want those category-type #tags pages to be hidden as well — but it’s too difficult for me.

As I mentioned before, I barely know anything about queries. I’ve tried a few things with AI’s help, but they probably won’t be of much use.

Under :where, replace this:

     [?p :block/name ?current]
     (pr ?p ?tag)

…with this:

     [?p :block/name ?current]
     (pr ?p ?tag)
     (not 
       [?tag-p :block/name ?tag]
       [?tag-p :block/properties ?props]
       [(get ?props :exclude-from-graph-view)]
     )
1 Like

Pages with the exclude-from-graph-view property still appear in the results :smiling_face_with_tear:

Right. I have updated my answer, try the new version.

1 Like

wow It works! really thank you
This will make my Logseq perfect :laughing: