10 December 2012

Search for multiple labels on Blogger

Labels for posts and displaying pages containing only articles with a specific label are pretty basic features for any blogging platform. But what if you want to search for several labels and retrieve articles that contain all those labels or any label? Is that even possible? I asked myself this at some point and a comment from yesterday prompted me to finally find the answer.

One of the solutions I discovered on the help forums uses search with a query in this format: <blog-URL>/search/?q=label:LABEL1+label:LABEL2. This returns posts with both labels. I tested it myself and it works, but it’s hard to manually build the URL and the search is case sensitive, meaning that it will return no results it you don’t input the label name exactly as created in Blogger.

While playing around on the blog I discovered an easier way to do this: modify the normal label URL in Blogger by adding a + sign followed by the name of the second label: <blog-URL>search/label/LABEL1+LABEL2. It’s easier to remember and to type, but it’s also case sensitive. It actually works with more than two labels – I only tested it with three, so I’m not sure if there is a limit to the number of labels you can add in the URL. I don’t use any labels with multiple words so I couldn’t test this case, but it’s safe to assume you need to escape spaces by replacing them with %20 just like Blogger does with the regular label pages.  Blogger search multiple labels

But what about the other case, when you want to find posts with any of those labels – the OR operator instead of AND? Unfortunately the second option doesn’t seem to support OR, but the first one does: just replace the + in the search query with a vertical bar like this: <blog-URL>/search/?q=label:LABEL1|label:LABEL2. It’s fun to see this in action especially with labels that have very little to do with one another.

22 comments:

  1. oh just did Thanks man you are the best !!

    ReplyDelete
  2. Works fine without "%20" to designate a space in a label with multiple words.

    ReplyDelete
  3. yes it is awesome. its 100%working.

    ReplyDelete
  4. I had to search so many authors to learn blogger template and its complexities. But now my journey has ended as I have reached an excellent author to learn anything in Blogger. I will be commenting here of and on and seeking guidance to move forward.

    ReplyDelete
  5. Could you tell me how this will work in the case of a space in your label eg: 'Lumia 920'

    I tried Lumia%20920 but that doesnt work for me

    ReplyDelete
    Replies
    1. That should work. Does the label exist?

      Delete
  6. this works well, but I also wish to add something to the page via a condition. I cannot get the condition to work. Do you know how? This is what I tried:

    ReplyDelete
    Replies
    1. I think the condition got stripped from the comment, but I found your question on StackOverflow. I'm not sure if Blogger supports on OR operator, so you should probably try to nest the conditions, something like:

      if cond='data:blog.searchLabel == Graphics' [code]
      else if cond='data:blog.searchLabel == Identity' [same code]
      else if cond='data:blog.searchLabel == Brand' [same code again]
      else [the other option]

      Not very efficient, but unfortunately I don't see another solution...

      Delete
  7. Is there any way in searching inside a specific label. So i have a label named label 1, i need to search articles only inside the label 1 and no other articles from any other labels should come.

    ReplyDelete
    Replies
    1. As far as I know there isn't, sorry...

      Delete
  8. Thanks. Works fine on my blog.

    ReplyDelete
  9. It Doesn't work with max-results=..... !!

    ReplyDelete
  10. Thank you! If works fine :)
    And you can use this method to divide your posts in categories. I´ve created a new link-tipe page whose url link is a search with two labels.
    Now, in my pages widget I have my blog posts divided properly :)

    In:
    http://inforusty.blogspot.com.es/

    One of the pages is:
    http://inforusty.blogspot.com.es/search/?q=label:me.sobra||label:me.falta

    So thank you again :)

    ReplyDelete
  11. The best and easiest way to search through labels is to add a menu bar on your home page. Just put the categories in the drop down menu and add the links through your labels. This will make refine and easy to track your blog posts under each label. Just check here And you will see how it works for me

    ReplyDelete