# Custom badge query is not targeting posts

**URL:** https://meta.discourse.org/t/custom-badge-query-is-not-targeting-posts/105515
**Category:** Support
**Created:** [January 2, 2019, 8:32pm UTC](https://meta.discourse.org/t/custom-badge-query-is-not-targeting-posts/105515 "2019-01-02T20:32:28Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Richie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richie/32/115110_2.png) [@Richie](https://meta.discourse.org/u/Richie)
#### Post date: [January 2, 2019, 8:32pm UTC](https://meta.discourse.org/t/custom-badge-query-is-not-targeting-posts/105515/1 "2019-01-02T20:32:28Z")

</div>

Hi everyone,

I’m running Discourse v2.2.0.beta6 +5 and seem to be having an issue when trying to link specific posts to a new badge, automatically.

I have created a badge called “Pin Seeker” and here’s a screen shot of the current settings for that badge:

 ![21](https://global.discourse-cdn.com/meta/original/3X/1/3/13c05b22b0a8b1bc5f1ae5bbd56e6712c5b444d9.png)

The following SQL is used to automatically issue the badge daily:

```plaintext
SELECT p.user_id, min(p.created_at) granted_at, MIN(p.id) post_id
FROM badge_posts p
JOIN topics t ON t.id = p.topic_id
WHERE category_id = (
  SELECT id FROM categories WHERE name ilike 'Where to fly your drone in the UK'
) AND p.post_number = 1 AND p.raw LIKE '%dronescene.co.uk%' 
GROUP BY p.user_id
```

So when someone starts a new topic in the category called “Where to fly your drone in the UK” and that topic also contains a link to `dronescene.co.uk` then we grant them the Pin Seeker badge.

This query works fine and the badge gets issued to the correct people each night 👍🏻

However, when viewing the badge itself, or the profile of people with the badge, there is no link to their topic.

Despite me ticking the “Query targets posts” check box (as you’ll see above). Other badges show the links correctly so this is not a site-wide issue.

e.g.:

 ![52](https://global.discourse-cdn.com/meta/original/3X/9/5/953e0a22e4202c5724bb6f92df94b3e49de678ce.png)

The strange thing is that when I click on “Preview granted badges” the links to the topic appear correctly in the preview:

 ![29](https://global.discourse-cdn.com/meta/original/3X/4/3/435cbb2f4132fbe73a3b3a86c60026f7861f8935.png)

Anyone come across this issue before?

Have I missed something in my badge SQL that ‘makes’ the links to the topics? 🤔

---

<div class="post-metadata">

### Author: ![Richie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/richie/32/115110_2.png) [@Richie](https://meta.discourse.org/u/Richie)
#### Post date: [January 2, 2019, 8:46pm UTC](https://meta.discourse.org/t/custom-badge-query-is-not-targeting-posts/105515/2 "2019-01-02T20:46:19Z")

</div>

D’oh!

I’ve just solved my own issue.

I’d missed this check box!

> [@What does "Show post granting badge on badge page" do?](https://meta.discourse.org/t/what-does-show-post-granting-badge-on-badge-page-do/43816):
>
> In Admin/Badges there is a checkbox option Show post granting badge on badge page I can’t figure out what it does - any clues?

Sorry 😊

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [February 1, 2019, 8:50pm UTC](https://meta.discourse.org/t/custom-badge-query-is-not-targeting-posts/105515/3 "2019-02-01T20:50:04Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
