Forum Discussion

adriana4u's avatar
3 years ago

Is there any API call to see how many posts have a tag?

Hello,

 

Is there any API call that we can use to see how many posts in our community have a certain tag?

 

Thank you ahead!

  • I think this should work. 

    SELECT count(*) FROM messages WHERE tags.text = 'wireless headphones'

     

    See here messages (khoros.com) for more options.

     

    So the full call would be something like this

    [Community Domain]/api/2.0/search?q=SELECT%20count(*)%20FROM%20messages%20WHERE%20tags.text%20%3D%20%27wireless%20headphones%27

     

    or with Freemarker

    rest("2.0","/search?q=" + "SELECT count(*) FROM messages WHERE tags.text = 'wireless headphones'"?url)

     

    • adriana4u's avatar
      adriana4u
      Maven

      Hello,

       

      Hope to find you well. 

       

      I have tried to do this api call:

       

       

      And it gave the error above... Maybe I'm not doing it well, since I've only made rest apis before.

      • Akenefick's avatar
        Akenefick
        Genius

        Hi,

        Try doing it through your browser while you are logged into your community in the same browser. 

        Also if you want more than the count you can add other fields like id to get a list of message ids for messages with that tag or view_href to get links to the posts. SELECT id, view_href, count(*) FROM messages etc.. Just need to encode everything after the equals sign, but it looks like you have that covered.

    • adriana4u's avatar
      adriana4u
      Maven

      Hello StanGromer,

       

      Thank you so much for your help!

       

      Issue is this tag is currently not being read in the community because it has a % on it. That's essentially the main issue. I want to see which posts have this tag to make a new one and delete the current one.

    • Akenefick's avatar
      Akenefick
      Genius

      Hi adriana4u

      See this on accessing API v2 Getting started with API v2 (khoros.com)

      In particular it says "On private communities where anonymous access is turned off, anonymous users cannot access the API."

      I'm guessing that is the case for you. Make sure you are logged into the community and try again. I think it should work if you are logged in on the same browser.

      Also I got I different error when I tried your query for my community. It looks like maybe your encoding is a little messed up. You mentioned you have a percent sign in your tag name. Is this the query you are trying?

      SELECT count(*), view_href FROM messages WHERE tags.text= 'CPU 100%'

      encoded that should be 

      SELECT%20count(*)%2C%20view_href%20FROM%20messages%20WHERE%20tags.text%3D%20%27CPU%20100%25%27

       

      This is a useful site for encoding URL Decoder/Encoder (meyerweb.com)

  • Hello !! It worked!!!!!  Thank you so much Akenefick 

     

    I just have an additional question! How do I add something to know which posts are these?

    • Akenefick's avatar
      Akenefick
      Genius

      So it looks like including the count(*) field prevents individual info about the messages from showing. So remove that and just use view_href if you want a link to each message.

      So:

      SELECT view_href FROM messages WHERE tags.text= 'CPU 100%'

      or encoded

      SELECT%20view_href%20FROM%20messages%20WHERE%20tags.text%3D%20%27CPU%20100%25%27

      The results you get will be a little hard to read. I use this site Json Parser Online.

      Is that what you were looking for?