How can I automatically grant a badge when the user visits a link within the discourse forum?
How about using the topic_link_clicks table?
I think I might have a manual badge that could benefit from this.
I’ll have a think.
If it’s a specific link shared in a specific post, I think this one would do it:
SELECT DISTINCT tlc.user_id, MIN(tlc.created_at) granted_at
FROM badge_posts p
LEFT JOIN topic_links tl ON tl.post_id = p.id
LEFT JOIN topic_link_clicks tlc ON tlc.topic_link_id = tl.id
WHERE tl.post_id = 1246
AND tl.url = 'https://meta.discourse.org/'
AND tlc.user_id IS NOT NULL
GROUP BY tlc.user_id
You’d need to grab the post_id of your specific post from its post json (or run a look up through the data explorer)
I think this could work.
I only want to grant this badge to members of our course.
Is there any way to hide a topic from the general public and only make it available to those who click on it via a link.
Secondly, how can this be done using Dfata explorer plugin?
I’m not 100% sure I’m following the method you’re using for this? You may want to have a check that you’re not overcomplicating the setup.
If checking the json isn’t your cup of tea, then a post_id lookup query would be something like this:
-- [params]
-- topic_id :topic_id
-- int :post_number
SELECT id
FROM posts
WHERE topic_id = :topic_id
AND post_number = :post_number
Then you can use the URL of the post to pull out the topic id and post number to enter into the parameter boxes. eg: https://meta.discourse.org/t/grant-badge-when-user-visits-link/276700/4 (worth noting that the OP is always post_number 1, even if it’s not in the topic link)
Actually, there’s a magic parameter that might make looking up a post_id using the data explorer even easier…
-- [params]
-- post_id :url
SELECT id
FROM posts
WHERE id = :url
Paste the relevant URL for the post into the parameter box, et voila - it pops out the post_id ![]()

The red background makes me think it’s not a full post URL. If it’s a topic URL you need to pop a /1 on the end:
https://meta.discourse.org/t/grant-badge-when-user-visits-link/276700/1
That worked!
Im assuming this needs to be replaced with the post URL including the ‘/1’?
That bit needs to be replaced with the link they’re clicking on within the post.
And the post_id is the post in which the link is pasted/shared.
Oh I see, so there is no way to just grant the badge just by them visiting the post link?
I think there should be, but that would be a different query.
This one awards a badge for clicking on a specific link in a specific forum post.
If you want to award a badge for a user visiting a particular topic I think it would be:
SELECT DISTINCT tv.user_id, MIN(tv.viewed_at) granted_at
FROM topic_views tv
WHERE tv.topic_id = 728
GROUP BY tv.user_id
(Replacing ‘728’ with the relevant topic_id)
As it needs the Update Daily trigger, it will be granted the next time the daily badge grant job runs. You can manually trigger Jobs::BadgeGrant from your /sidekiq/scheduler page if you want to speed it up to check.
How can I make it update instantly?
The only available triggers are:
- Update daily
- When a user edits or creates a post
- When a user is edited or created
- When a user acts on a post
- When a user is edited or created
- When a user changes trust level

To have a more instant badge granted, you would need to work within the confines of those actions. You can read more in this topic - Create Triggered Custom Badge Queries
I did this but still not been granted yet.
Yes it’s enabled. Let me try on another profile. Maybe its because I am the admin.
Secondly, are you just pasting the topic link into your browser?
Being admin doesn’t make a difference for this one.
You would need to visit the topic for it to register in the topic_views table this badge is based on.



