# Receive a badge for a post into a topic containing specific tag

**URL:** https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397
**Category:** Support
**Created:** [November 9, 2015, 9:07pm UTC](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397 "2015-11-09T21:07:42Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![alefattorini](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alefattorini/32/119928_2.png) [@alefattorini](https://meta.discourse.org/u/alefattorini)
#### Post date: [November 9, 2015, 9:07pm UTC](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397/1 "2015-11-09T21:07:42Z")

</div>

I’d like to assign a badge when my members reply to a topic with the “welcoming” tag  
How can I do this?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 3, 2016, 9:37pm UTC](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397/2 "2016-02-03T21:37:17Z")

</div>

That’s exactly what I want to do too.

## Where are tags in the database?

I’ve poked in [data explorer](https://meta.discourse.org/t/32566?silent=true) and can’t find “tag” in it anywhere. [badge-query-request-received-x-likes-in-topic-containing-specific-tag](https://meta.discourse.org/t/badge-query-request-received-x-likes-in-topic-containing-specific-tag/27901) asks the same question, but the answer worked another way.

## Why badges for tags?

Others say that badges for posting is ripe for abuse, but I have students create a topic with a certain tag to indicate that they have completed an assignment. I want to create a badge so that they can see that they have “turned in” the work, and then another to indicate that it was “good” (presumably with a “like” until I get a you-did-it-right plugin written).

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [February 3, 2016, 10:35pm UTC](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397/4 "2016-02-03T22:35:08Z")

</div>

> [@pfaffman](#):
>
> Where are tags in the database?

Since tags are in the discourse-tagging plugin, its data is stored in the X\_custom\_fields tables.

## Badge for topic in CATEGORY-NAME with tag TAG-NAME

```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
JOIN topic_custom_fields tcf on t.id = tcf.topic_id
WHERE category_id = (
  SELECT id FROM categories WHERE name ilike 'CATEGORY-NAME'
) AND p.post_number = 1
AND tcf.name LIKE 'tags' and tcf.value LIKE 'TAG-NAME'
and (:backfill OR ( p.id IN (:post_ids) ))
GROUP BY p.user_id

```

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

### How to get it to run on the posts that are already there.

1. Visit [https://HOSTNAME/sidekiq/scheduler](https://HOSTNAME/sidekiq/scheduler)

2. Find `Jobs::BadgeGrant`

3. Click `trigger` button

### P.S. How to select across multiple categories?

without `error more than one row returned`? Like this:

```plaintext
...
WHERE category_id = ANY (
  SELECT id FROM categories WHERE name ilike '%completions%'
...

```

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 31, 2016, 6:55pm UTC](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397/5 "2016-05-31T18:55:20Z")

</div>

Just a note that since [Tagging support is now part of discourse](https://meta.discourse.org/t/tagging-support-is-now-part-of-discourse/43334), the way to create a badge for a post with a given tag has changed.

The code now should look like this:

```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
JOIN topic_tags tt on t.id = tt.topic_id
JOIN tags on tags.id = tt.tag_id
WHERE category_id = (
  SELECT id FROM categories WHERE name ilike 'CATEGORY-NAME'
) AND p.post_number = 1
AND tags.name LIKE 'TAG-NAME'
and (:backfill OR ( p.id IN (:post_ids) ))
GROUP BY p.user_id

```

Changing all of your Badge Queries is left as an exercise to the admin. 🙂

(But I’ve got dozens of badges to change, so I’ll probably have a script that will fix at least mine Real Soon Now. If you’re interested, let me know and I’ll endeavor to make it useful for others.)

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [June 9, 2016, 4:13pm UTC](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397/6 "2016-06-09T16:13:44Z")

</div>

If anyone’s interested, I’ve got a Python script that will get all of the badges, look for “tcf.value” in the query and replace the tcf.value queries with those using the new tables.

---

<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: [January 29, 2019, 1:54pm UTC](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397/7 "2019-01-29T13:54:57Z")

</div>



---

<div class="post-metadata">

### Author: ![bartv](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/bartv/32/130052_2.png) [@bartv](https://meta.discourse.org/u/bartv)
#### Post date: [April 22, 2019, 7:03am UTC](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397/8 "2019-04-22T07:03:55Z")

</div>

Side note that this query will only return the latest post that qualifies. If you have a badge that can be awarded multiple times, use the following version:

```plaintext
SELECT p.user_id, p.created_at granted_at, p.id post_id
FROM badge_posts p
JOIN topics t ON t.id = p.topic_id
JOIN topic_tags tt on t.id = tt.topic_id
JOIN tags on tags.id = tt.tag_id
AND tags.name LIKE 'featured'
and (:backfill OR ( p.id IN (:post_ids) ))
AND p.post_number = 1

```

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [July 25, 2022, 7:14am UTC](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397/10 "2022-07-25T07:14:43Z")

</div>


