# 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:** Product Support\
**Created:** [9 Novembro , 2015 21:07 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:** 1\
**Showing post:** 4

<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:** [3 Fevereiro , 2016 22:35 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%'
...

```

---

_[View the full topic](https://meta.discourse.org/t/receive-a-badge-for-a-post-into-a-topic-containing-specific-tag/35397)._
