Mass updating wordpress posts with a Songkick artist ID

I recently had the task of updating a wordpress site to show the latest tour dates for artists on http://feelthebeatz.com.

The site had over 100 posts on different DJs so I needed an automated way of showing updated tour dates as a widget in the right hand column.

Songkick has an API that provides tour date info in the form of a JSON response if you make the request with an artist ID – problem was that all I had was a DJ name in the subject line of each post.

This is what I did so that only one call was made to songkick for tour dates.

Signup for a songkick developer API key

http://www.songkick.com/developer

You’ll need to provide a website that can be approved by songkick and then a few days later you should be sent a developer key.

How to get the artist ID for each DJ?

The request to songkick to get a JSON response for artist info is in the form of:
[code]]czoxMDc6XCINCmh0dHA6Ly9hcGkuc29uZ2tpY2suY29tL2FwaS8zLjAvc2VhcmNoL2FydGlzdHMuanNvbj9xdWVyeT1bYXJ0aXN0IG57WyYqJl19YW1lXSZhbXA7YXBpa2V5PVt5b3VyIGRldmVsb3BlciBrZXldDQpcIjt7WyYqJl19[[/code] [artist name] is used to search the songkick database to return one or more artist entries.

[developer key] is the api key received from songkick.

The idea was to use the title of a wordpress post as the [artist name], and then added a custom field to the post when an artist id was returned.

Here’s the function to set the artist ID in a post’s custom field.

[code]]czo3MDE6XCINCmZ1bmN0aW9uIHNldF9zb25na2lja2lkX2Zyb21fdGl0bGUoJHBvc3RfSUQpDQp7DQoJJHRpdGxlID0gZ2V0X3RoZV97WyYqJl19dGl0bGUoJHBvc3RfSUQpOw0KCSR1cmx0aXRsZSA9IHVybGVuY29kZSgkdGl0bGUpOw0KDQoJLy9idWlsZCB0aGUgdXJsIHJlcXVlc3tbJiomXX10DQoJJGpzb251cmwgPSBcJ2h0dHA6Ly9hcGkuc29uZ2tpY2suY29tL2FwaS8zLjAvc2VhcmNoL2FydGlzdHMuanNvbj9xdWVyeT1cJy57WyYqJl19JHVybHRpdGxlLlwnJmFwaWtleT1beW91ciBkZXZlbG9wZXIga2V5XVwnOw0KDQoJJGpzb24gPSBmaWxlX2dldF9jb250ZW50cygkanNve1smKiZdfW51cmwpOw0KCSRqc29uYXJyID0ganNvbl9kZWNvZGUoJGpzb24sdHJ1ZSk7IC8vYXMgYW4gYXNzb2NpYXRpdmUgYXJyYXkNCg0KCSR7WyYqJl19cmVzdWx0c2FyciA9ICRqc29uYXJyW1wicmVzdWx0c1BhZ2VcIl1bXCJyZXN1bHRzXCJdOw0KDQoJLy9jaGVjayBpZiBhbnkgcmVzdWx0cyB7WyYqJl19cmV0dXJuZWQNCglpZihjb3VudCgkcmVzdWx0c2FycikgPiAwKQ0KCXsNCgkJJHNvbmdraWNraWQgPSAkcmVzdWx0c2FycltcImFydGl7WyYqJl19c3RcIl1bMF1bXCJpZFwiXTsNCgkJYWRkX3Bvc3RfbWV0YSgkcG9zdF9JRCxcJ3NvbmdraWNraWRcJywkc29uZ2tpY2tpZCk7DQoJCXJldHVye1smKiZdfW4gXCJQb3N0OiBcIi4kdGl0bGUuXCIgc29uZ2tpY2tJRDpcIi4kc29uZ2tpY2tpZDsNCgl9DQoJcmV0dXJuICBcIlBvc3Q6IFwiLiR0aXRsZS57WyYqJl19XCIgc29uZ2tpY2tJRDpub25lXCI7DQp9DQpcIjt7WyYqJl19[[/code]

Cycling through all the posts

The above code updates a given post, so to update all posts I used the following:
[code]]czo0NDc6XCINCmZ1bmN0aW9uIHNldF9zb25na2lja2lkX2FsbF9wb3N0cygpDQp7DQoJZ2xvYmFsICRwb3N0Ow0KCSRhcmdzID0gYXJ7WyYqJl19cmF5KCBcJ251bWJlcnBvc3RzXCcgPT4gLTEsIFwnb3JkZXJieVwnPT4gXCdJRFwnICk7DQoJJHBvc3RzID0gZ2V0X3Bvc3RzKFwnbnVtYmVycG97WyYqJl19c3RzPS0xXCcpOw0KDQoJZm9yZWFjaCgkcG9zdHMgYXMgJHBvc3QpIDogc2V0dXBfcG9zdGRhdGEoJHBvc3QpOw0KCQkvL2NoZWNrIGl7WyYqJl19ZiBjdXN0b20gZmllbGQgaGFzIG5vdCBiZWVuIHNldA0KCQkkc29uZ2tpY2tpZCA9IGdldF9wb3N0X21ldGEoJHBvc3QtPklELFwnc297WyYqJl19bmdraWNraWRcJywgdHJ1ZSk7DQoJCWlmKCRzb25na2lja2lkID09IFwnXCcpDQoJCXsNCgkJCSRyZXN1bHQgPSBzZXRfc29uZ2tpY2tpZHtbJiomXX1fZnJvbV90aXRsZSgkcG9zdC0+SUQpOw0KCQkJZWNobyBcJzxiciAvPlwnLiRyZXN1bHQ7DQoJCX0NCgllbmRmb3JlYWNoOw0KfQ0KXCI7e1smKiZdfQ==[[/code]

Running the code over wordpress

This is a long running process as each request needs to be made to songkick, so the code is not really suited to attaching the plugin to an action – it really needed to be separate from normal WordPress functionality for a one-time update.

I created a separate php file in the root of the site that includes the necessary wordpress files in order to access wordpress functions.

[code]]czoxMjU6XCINCnJlcXVpcmVfb25jZSggZGlybmFtZShfX0ZJTEVfXykgLiBcJy93cC1sb2FkLnBocFwnICk7DQpyZXF1aXJlX29uY2UoIHtbJiomXX1kaXJuYW1lKF9fRklMRV9fKSAuIFwnL3dwLWFkbWluL2FkbWluLWZ1bmN0aW9ucy5waHBcJyApOw0KXCI7e1smKiZdfQ==[[/code]

Here’s the full code for the file that outputs the post title and artist ID for each when you access the file through the browser.
[code]]czoxMzExOlwiDQo8P3BocA0KcmVxdWlyZV9vbmNlKCBkaXJuYW1lKF9fRklMRV9fKSAuIFwnL3dwLWxvYWQucGhwXCcgKTsNCnJlcXVpcntbJiomXX1lX29uY2UoIGRpcm5hbWUoX19GSUxFX18pIC4gXCcvd3AtYWRtaW4vYWRtaW4tZnVuY3Rpb25zLnBocFwnICk7DQoNCnNldF9zb25na2l7WyYqJl19Y2tpZF9hbGxfcG9zdHMoKTsNCg0KZnVuY3Rpb24gc2V0X3NvbmdraWNraWRfYWxsX3Bvc3RzKCkNCnsNCglnbG9iYWwgJHBvc3Q7DXtbJiomXX0KCSRhcmdzID0gYXJyYXkoIFwnbnVtYmVycG9zdHNcJyA9PiAtMSwgXCdvcmRlcmJ5XCc9PiBcJ0lEXCcgKTsNCgkkcG9zdHMgPSBnZXRfcG9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XCJdW1wicmVzdWx0c1wiXTsNCg0KCS8vY2hlY2sgaWYgYW55IHJlc3Vse1smKiZdfXRzIHJldHVybmVkDQoJaWYoY291bnQoJHJlc3VsdHNhcnIpID4gMCkNCgl7DQoJCSRzb25na2lja2lkID0gJHJlc3VsdHNhcnJbXCJhe1smKiZdfXJ0aXN0XCJdWzBdW1wiaWRcIl07DQoJCWFkZF9wb3N0X21ldGEoJHBvc3RfSUQsXCdzb25na2lja2lkXCcsJHNvbmdraWNraWQpOw0KCQlyZXtbJiomXX10dXJuIFwiUG9zdDogXCIuJHRpdGxlLlwiIHNvbmdraWNrSUQ6XCIuJHNvbmdraWNraWQ7DQoJfQ0KCXJldHVybiAgXCJQb3N0OiBcIi4kdGl0e1smKiZdfWxlLlwiIHNvbmdraWNrSUQ6bm9uZVwiOw0KfQ0KXCI7e1smKiZdfQ==[[/code] Where an artist ID was not found, you should just see a ‘none’ output.

You may have to run the file a few times if the script times out due to the max php timeout being exceeded.
A check is made on each post to see if the artist id custom field has already been set.

Displaying the tour dates for each artist

Now comes the easy part.

There is a songkick plugin that displays tour dates for a given artist ID at http://wordpress.org/plugins/songkick-concerts-and-festivals/ by using a shortcode.

I placed the following into a text widget with php and shortcodes enabled.

[code]]czozMDE6XCINCjw/cGhwIA0KaWYoaXNfc2luZ2xlKCkpDQp7DQpnbG9iYWwgJHBvc3Q7DQokc29uZ2tpY2tpZCA9IGdldF9wb3N0X217WyYqJl19ZXRhKCRwb3N0LT5JRCxcJ3NvbmdraWNraWRcJywgdHJ1ZSk7DQokb3V0cHV0ID0gZG9fc2hvcnRjb2RlKFwnW3NvbmdraWNrX2NvbmNlcntbJiomXX10c19hbmRfZmVzdGl2YWxzIHNvbmdraWNrX2lkPVwnLiRzb25na2lja2lkLlwnIHNvbmdraWNrX2lkX3R5cGU9YXJ0aXN0XVwnKTsgDQple1smKiZdfWNobyAkb3V0cHV0Ow0KfQ0KZWxzZSB7DQplY2hvIFwiQnJvd3NlIHRvIGEgREogdG8gZ2V0IHRoZWlyIHRvdXIgZGF0ZXNcIjsNCn0NCntbJiomXX0/Pg0KXCI7e1smKiZdfQ==[[/code]

The widget now displays tour dates for each artist post based on the custom field artist ID we populated.

The songkick plugin only needs to make 1 call to songkick now, and results are cached.

Have a look at this page to see the result.

http://feelthebeatz.com/tiesto/

Click Here to Leave a Comment Below

Leave a Reply: