I think I have figured out one method to retrieve topics based on a custom field: search for them and retrieve the results.
Here’s an example, imagining you have a button with the action “searchForTopics()” somewhere, and you are trying to get back topics with the custom field fun_level
equal to “super-duper-fun”:
(this would go in some js.es6 code, like an initializer):
import Topic from 'discourse/models/topic'; //not necessary based on the code below, but probably relevant for other related actions you'd want to take
import { ajax } from 'discourse/lib/ajax';
export default {
actions: {
checkTopic(){
let custom_field_value = 'super-duper-fun'
let searchTerm = 'fun_level:' + custom_field_value
let args = { q: searchTerm }
ajax("/search", { data: args }).then(results => {
let topics = results.topics
topics.forEach(topic => {
//just to get a list of the topic names
console.log('topic name = ')
console.log(topic)
})
})
}
}
}
This works. But is this the most efficient way to do it?
For example, is this method, using search, as efficient as the method discourse uses to show you topics that match a certain tag when you go to that tag page?