How do I exclude certaing tag when using the search API?

Posts: 2 · Views: 74
  • 34438

    How do I exclude certain tags when using the search API?

    I want to exclude the "digital art" tag which is "479".

    First I tried this after reading the API documentation:

    https://wallhaven.cc/api/v1/search/?categories=100&purity=100&sorting=toplist&topRange=3M&q=-479

    Then I searched the forums and tried this

    https://wallhaven.cc/api/v1/search/?categories=100&purity=100&sorting=toplist&topRange=3M&q=-c:479

    None of them excluded the "digital art" pictures from the results.

    Last updated
  • 34978

    Yeah, Lord knows it's not exactly obvious even after reading the documentation available, it took me adding some intuition and experience from previous projects interacting with them to arrive at the correct rubric. Hopefully at some point the documentation itself gets some sorely needed TLC or is made open-source where those who may have improvements to offer can do so as they see fit.

    The correct syntax is to add a q=-%7Btag%20name%7D parameter to your API call. Breaking that down, we have the parameter name, q, separated from the parameter value by the equality symbol (= or %3D when optionally URL-encoded), a hyphen-minus (-) and finally the name of the tag and not its tag ID number. Further complicating matters is the manifold instances of tags whose name is not a single word. For those, we enclose them in curly braces and place the full tag name inside, spaces and all, then URL-encode the whole enchilada. That's how we arrive at my example in the first sentence, where %7B is a URL-encoded {, and likewise with %20 for a space as well as %7D for the terminating curly brace, }.

    I've seen others on this forum state the the curly braces are unnecessary, or that the URL-encoded space can be substituted much more easily with a simple underscore (_) character, and they're not exactly wrong, but not completely right, either, in my experience at least. There are times that I've taken their advice on how to simplify the syntax and it did indeed work, but only if the API call in whole was relatively simple and uncluttered. The instant I tried to follow their suggestions when requesting search results from a query with a variety of different parameters involved, or even just an ample supply of q parameters mixing inclusive and exclusive tag names, it completely broke down.

    Once I arrived at the rubric described above, it's been nothing but smooth sailing for me with the API here. In fact, one of my simple pleasures in life is the presence of the Variety program on my Linux environments and WPC on Windows, both of which use fairly intricate API calls involving no less than half a dozen tags to surprise me with a new desktop wallpaper from here that suits my tastes once every 45 minutes. I hope this is helpful to you and perhaps anyone else with similar questions, though I feel I should make it clear that I'm nothing more than just another ordinary user with no authority to speak for the site or its owners. Everything I've shared is purely the result of trial and error on my part and I have no concrete knowledge of the infrastructure on which the site and API rest; if I'm in error I hope someone pops in and corrects me so that we can both learn something.

    Best wishes to you.

Message