Questioner
Questioner

Reputation: 7463

How do I filter a Blogger feed by label?

I am using Google's Blogger API to retrieve items from my Blogger blog to display on my home page. I'm using Javascript made available on the Blogger Developer's Blog. What I'd like to be able to do is filter entries returned by label. I've done a lot of searching but, even though it seems this should be simple, I haven't found any clear directions.

My code to retrieve the Blogger entries looks like this:

function init() {
    // Get your API key from http://code.google.com/apis/console
    gapi.client.setApiKey('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
    // Load the Blogger JSON API
    gapi.client.load('blogger', 'v3', function() {
        // Load the list of posts for code.blogger.com
        var request = gapi.client.blogger.posts.list({
            'blogId': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            'fields': 'items(content,title,updated,id,labels)',
            'fetchBodies': true
        });
        request.execute(function(response) {

            var blogger = document.getElementById("blogger");
            var anchor = 0;
            for (var i = 0; i < response.items.length; i++)
            {
                var bloggerDiv = document.createElement("div");
                bloggerDiv.id = "blogger-" + i;
                bloggerDiv.className = "bloggerItem";

                // For the parts of the response, have a look at the result at:
                // http://code.google.com/apis/explorer/#_s=blogger&_v=v2&_m=posts.list&blogId=xxxxxxxxxxxxx
                $(bloggerDiv).append("<h2>" + response.items[i].title + "</h2>");
                var date = response.items[i].updated;
                date = date.replace("T", " ");
                date = date.replace("+03:00", "");
                var printDate = new moment(date);
                $(bloggerDiv).append("<p><span class='byline'>" + printDate.format('dddd, MMMM Do YYYY, h:mm:ss a') + "</span></p>");
                $(bloggerDiv).append(response.items[i].content);

// This logic might be useful to keep around if I decide to do
// something different based on whether or not the last item
// needs different formatting in some way.
//        if (i+1<response.items.length) {
//          $(bloggerDiv).append("<hr>");
//        }
//        $(bloggerDiv).append("<hr>");


                bloggerAnchor = document.createElement("a");

                bloggerAnchor.name = "blogger-" + response.items[i].id;

                blogger.appendChild(bloggerAnchor);
                blogger.appendChild(bloggerDiv);

                anchor = anchor + 1;

            }
            // find out which anchor the user wanted...
            var hashVal = window.location.hash.substr(1);
// ... then jump to that position:
            location.hash = "#" + hashVal;
        });
    });
}

This returns everything on the blog. How to I get it to only return entries where label="XYZ"?

I thought this would be answered by now, but the one question I found on SO that seems related contains links that are now dead, so whatever information they held is now unavailable.

Upvotes: 0

Views: 1211

Answers (1)

Questioner
Questioner

Reputation: 7463

Finally found it. Turns out this was as easy as I thought.

All I had to do was add this line of code into the request section:

    var request = gapi.client.blogger.posts.list({
        'blogId': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'fields': 'items(content,title,updated,id,labels)',
        'labels': 'XYZ',
        'fetchBodies': true
    });

Couple of things that tripped me up were that the field is 'labels', plural, not 'label', singular. Also, for some reason, it seems that the 'labels' specification has to come before 'fetchBodies', or the code fails. I'm not sure why that would be, but that was my experience.

Upvotes: 2

Related Questions