# Topic ID as a variable?

**URL:** https://meta.discourse.org/t/topic-id-as-a-variable/374168
**Category:** Development
**Created:** [July 14, 2025, 6:48pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168 "2025-07-14T18:48:32Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [July 14, 2025, 6:48pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/1 "2025-07-14T18:48:32Z")

</div>

Is there a way to get the topic ID as a variable within a topic?

Use case, we have categories for support and bug tracking tickets. I’d like to set them up with templates like so.  
This:

```plaintext
> Do **not** remove this line: Support request #$(topicid)

```

Looks like this:

> Do **not** remove this line: Support request #246

Open to other ways to get the info. Just not sure how to 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: [July 14, 2025, 7:26pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/2 "2025-07-14T19:26:34Z")

</div>

You’re trying to put the topic ID in the raw text of the topic?

What problem is that going to solve?

---

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [July 14, 2025, 7:34pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/3 "2025-07-14T19:34:05Z")

</div>

It will solve the problem of me telling my support staff that they need to first create a new topic, then save it, then look at the URL and get the ID #, then go back and edit the topic in order to put the # in the body manually.

---

<div class="post-metadata">

### Author: ![jordan.vidrine](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jordan.vidrine/32/515563_2.png) [@jordan.vidrine](https://meta.discourse.org/u/jordan.vidrine)
#### Post date: [July 14, 2025, 7:42pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/4 "2025-07-14T19:42:39Z")

</div>

What are you using the topic ID# in the body of the OP for in your use case here?

---

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [July 14, 2025, 7:44pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/5 "2025-07-14T19:44:55Z")

</div>

A unique id # for a ticket/bug.

---

<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: [July 14, 2025, 8:11pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/6 "2025-07-14T20:11:50Z")

</div>

Where is that ticket/bug going? The ID is in the URL; there it is persistent and never wrong. How does it help to have the topic ID in the text of the topic? And, as you point out, if it’s in the body of the message, anyone can change, delete, or type it wrong.

Is something else using the topic ID? If so, what and how?

---

<div class="post-metadata">

### Author: ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)
#### Post date: [July 14, 2025, 8:42pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/7 "2025-07-14T20:42:44Z")

</div>

It will be easier to create a custom component that appends this to the correct posts rather than filling a template.

If you edit the code of your theme under `admin > themes & components`… and add this to the JS tab, it should get you what you’re after with some adjustment:

```gjs
import { apiInitializer } from "discourse/lib/api";
import Component from "@glimmer/component";

class TopicIdentifier extends Component {
  get topicId() {
    return this.args.post?.topic?.id;
  }

  get shouldShow() {
    const firstPost = this.args.post?.post_number === 1; // first post in topic
    const desiredCategories = [45, 3]; // a comma separated list of category IDs you want this to appear in 
    const isInCategory = desiredCategories.includes(
      this.args.post?.topic.category.id
    );

    return firstPost && isInCategory;
  }

  <template>
    {{#if this.shouldShow}}
      <!-- you can edit the content below, {{this.topicId}} is where the topic ID will be filled -->
      This topic's ID is
      {{this.topicId}} 
      <!-- you can edit the content above -->
    {{/if}}
  </template>
}

export default apiInitializer((api) => {
  api.decorateCookedElement((element, helper) => {
    const wrapper = document.createElement("div");
    wrapper.className = "custom-topic-id"
 
    helper?.renderGlimmer(
      wrapper,
      <template><TopicIdentifier @post={{helper.model}} /></template>
    );

    element.appendChild(wrapper);
  });
});

```

You can find the category ID from its URL to configure `desiredCategories`, for example, the [support category](https://meta.discourse.org/c/support/6) here on Meta has the ID of 6

You can add CSS to style this how you’d like with the CSS tab… for example

```css
.custom-topic-id {
  display: inline-block;
  background: yellow;
}

```

 ![A screenshot depicts a social media post on a platform like Reddit, featuring a bizarre and lengthy text post from a new user named Tobias, with a humorous caption asking for the funniest thing seen that day. (Captioned by AI)](https://global.discourse-cdn.com/meta/original/4X/5/d/6/5d6727d89e95e2f823557eae340585bbb739dbef.png)

---

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [July 14, 2025, 8:50pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/8 "2025-07-14T20:50:13Z")

</div>

Yes, it can be typed wrong. Hence why if you use a variable you don’t have to worry about typing it wrong.

It helps because I’m the user and that’s how I want my workflow set up.

You cannot currently search Discourse by topic id. If the # is in the body of the topic, then it’s now part of the search results.

So I can tell a coworker, 'Hey search for bug #138 and let me know what you think."

Then it’s really easy for Nancy to just go to the site and do a search for 138 and get a result.

---

<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: [July 14, 2025, 9:01pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/9 "2025-07-14T21:01:34Z")

</div>

> [@tknospdr](#):
>
> You cannot currently search Discourse by topic id.

You don’t need to search:

`/t/-/id` – but really `/t/id` works.

> [@tknospdr](#):
>
> 'Hey search for bug #138 and let me know what you think."

They can just type `/t/138` and not have to search. But if you are talking to a person, wouldn’t saying "search for bug about “some words they know” make more sense to use in conversation than a number?

> [@awesomerobot](#):
>
> It will be easier to create a custom component that appends this to the correct posts rather than filling a template.

And that was going to be my other suggestion.

I think I’d have used `renderInOutlet` rather than `decorateCookedElement`, but that’s probably because I don’t fully understand decorateCookedElement.

But this will don’t let you search for a bug/topic by topic\_id.

---

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [July 14, 2025, 9:04pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/10 "2025-07-14T21:04:40Z")

</div>

I will test this out. Thanks for the info. Is that part of the body that becomes searchable?

---

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [July 14, 2025, 9:20pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/11 "2025-07-14T21:20:10Z")

</div>

> [@pfaffman](#):
>
> You don’t need to search:
> 
> > /t/-/id`– but really`/t/id` works.

But I want to search. It’s easier.  
Go to site, then append /t/id to the URL at the top of the window VS go to site and type ID into the search field that’s right in the center of the page.

> [@](#):
>
> They can just type `/t/138` and not have to search. But if you are talking to a person, wouldn’t saying "search for bug about “some words they know” make more sense to use in conversation than a number?

If I’m talking to a person, or typing a message, yes a number is more concise and less error prone than “Search for exact title that might be 5 words long”

> [@](#):
>
> > [@awesomerobot](#):
> >
> > It will be easier to create a custom component that appends this to the correct posts rather than filling a template.
> 
> And that was going to be my other suggestion.
> 
> I think I’d have used `renderInOutlet` rather than `decorateCookedElement`, but that’s probably because I don’t fully understand decorateCookedElement.
> 
> But this will don’t let you search for a bug/topic by topic\_id.

I will look into that, but if it kills being able to use in a search field, then it won’t work for me.

I really love Discourse, and I have gotten a TON of useful feedback and help from everyone here.  
What I don’t understand is that occasionally folks tell me that “You shouldn’t do it that way because this way is better.” Well, no, this way is better for you, but that way is better for me. And neither is wrong.

---

<div class="post-metadata">

### Author: ![awesomerobot](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/awesomerobot/32/142900_2.png) [@awesomerobot](https://meta.discourse.org/u/awesomerobot)
#### Post date: [July 14, 2025, 9:27pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/12 "2025-07-14T21:27:12Z")

</div>

> [@tknospdr](#):
>
> I will look into that, but if it kills being able to use in a search field, then it won’t work for me.

Yeah don’t bother then, unfortunately it only appends content to the topic when it’s rendered… it doesn’t add it to the database to make it searchable. I think what you’d ultimately need is a custom plugin or a custom script added to [Discourse Automation](https://meta.discourse.org/t/discourse-automation/195773) for it to be fully automatic.

Searching by topic ID or including it in a template isn’t a common request, so it’s not something we support out of the box.

> [@tknospdr](#):
>
> What I don’t understand is that occasionally folks tell me that “You shouldn’t do it that way because this way is better.” Well, no, this way is better for you, but that way is better for me. And neither is wrong.

I think it’s that regulars here know it’s not a feature we currently have, so we’re trying to support you by figuring out some potential work-arounds.

---

<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: [July 14, 2025, 9:52pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/13 "2025-07-14T21:52:42Z")

</div>

> [@awesomerobot](#):
>
> so we’re trying to support you by figuring out some potential work-arounds.

And often if you go back you can find a better solution.

If you really want to embed them topic id in the text so you can find it in search then you’d need a plugin and it could add ticket123 to the raw text (I think search looks at raw?). The topic id doesn’t exist before you create the topc, so you can’t insert it when you create it.

Oh! Maybe what you want is a plugin that would add a tag bug123 when a topic in a bug category gets created. Then you could refer to it as `#bug123` and have it link anywhere you mention it and be able to search. If you’re self hosted or on enterprise that wouldn’t be too hard. I imagine someone in #Marketplace would do it for $250 to $500.

Or, if you want to be able to “search” for a bug by number, you could add a theme component that let you type it in a ‘bug box’ and go straight there. Maybe that’s a solution that would work and it wouldn’t require a plugin.

---

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [September 25, 2025, 3:52pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/14 "2025-09-25T15:52:20Z")

</div>

What I ended up with is a more discreet showing of the topic ID. It’s still not searchable, but I’ll live with it until what I really need becomes a reality.

```plaintext
<script type="text/discourse-plugin" version="0.5">
api.decorateWidget('post-meta-data:after', dec => {
  if (dec.attrs.post_number === 1) {
    const topic = dec.getModel().get('topic');
    return dec.rawHtml(`<div class="post-info topic-id"><b>&nbsp#${topic.get('id')}</b></div>`);
  }
});
</script>

```

Problem is that this code is now outdated and I don’t know how to fix it.  
What should it be updated to in order to be compliant?

---

<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: [September 25, 2025, 7:00pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/15 "2025-09-25T19:00:05Z")

</div>

I created [GitHub - literatecomputing/discourse-custom-components · GitHub](https://github.com/literatecomputing/discourse-custom-components) as an example of how to do that.

> <https://github.com/literatecomputing/discourse-custom-components/blob/main/javascripts/discourse/api-initializers/custom-components.gjs#L13-L18>

Is a trivial example of inserting a component. So you would add some code before `<template>` to get the topic id and logic for whether to display.

I think you might want `topic-list-before-link` as the plugin outlet.

---

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [September 26, 2025, 12:41pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/16 "2025-09-26T12:41:34Z")

</div>

I don’t understand any of that. I’m not a coder.  
The code I posted does this:

 ![topic-id](https://global.discourse-cdn.com/meta/original/4X/5/8/e/58e77dea27c50e27f3dfd1ebf6bc6a16cf6e39c7.jpeg)  
How would I do that with your code?

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [September 26, 2025, 2:05pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/17 "2025-09-26T14:05:09Z")

</div>

> [@tknospdr](#):
>
> But I want to search. It’s easier.  
> Go to site, then append /t/id to the URL at the top of the window VS go to site and type ID into the search field that’s right in the center of the page.

If I’m not wrong, inputting `topic:id` into the searchbar will show the topic in the results.

E.g. searching `topic:374168 this` brings up posts from this topic only.

> [@pfaffman](#):
>
> you’d need a plugin and it could add ticket123 to the raw text (I think search looks at raw?). The topic id doesn’t exist before you create the topc, so you can’t insert it when you create it

I wonder… would a TC that sends an ajax request to update the topic’s raw suffice?

---

<div class="post-metadata">

### Author: ![tknospdr](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tknospdr/32/529762_2.png) [@tknospdr](https://meta.discourse.org/u/tknospdr)
#### Post date: [September 26, 2025, 2:23pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/18 "2025-09-26T14:23:50Z")

</div>

> [@NateDhaliwal](#):
>
> E.g. searching `topic:374168 this` brings up posts from this topic only.

That only works for this topic, try a different random one. Try searching for “one two four two four zero” that’s the tag banners topic ID.

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [September 26, 2025, 2:31pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/19 "2025-09-26T14:31:43Z")

</div>

You mean `topic:124240`? The filler word (‘hello’) was just an example. In the case of Tag Banners, ‘hi’ works. Maybe if you have a common word across support tickets, or just a common word in general, you can try that? Just suggesting a workaround :slight_smile: .

---

<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: [September 26, 2025, 2:41pm UTC](https://meta.discourse.org/t/topic-id-as-a-variable/374168/20 "2025-09-26T14:41:52Z")

</div>

> [@tknospdr](#):
>
> Problem is that this code is now outdated and I don’t know how to fix it.

Did you try the code in [Topic ID as a variable? - #7 by awesomerobot](https://meta.discourse.org/t/topic-id-as-a-variable/374168/7)

Also, if what you want to do is “search” for a topic id, then you could probably make a component that accepted a topic ID and then just jumped to that topic.

[Next page](https://meta.discourse.org/t/topic-id-as-a-variable/374168.md?page=2)
