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:


http://api.songkick.com/api/3.0/search/artists.json?query=[artist name]&apikey=[your developer key]

[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.


function set_songkickid_from_title($post_ID)
{
  $title = get_the_title($post_ID);
  $urltitle = urlencode($title);

  //build the url request
  $jsonurl = 'http://api.songkick.com/api/3.0/search/artists.json?query='.$urltitle.'&apikey=[your developer key]';

  $json = file_get_contents($jsonurl);
  $jsonarr = json_decode($json,true); //as an associative array

  $resultsarr = $jsonarr["resultsPage"]["results"];

  //check if any results returned
  if(count($resultsarr) > 0)
  {
    $songkickid = $resultsarr["artist"][0]["id"];
    add_post_meta($post_ID,'songkickid',$songkickid);
    return "Post: ".$title." songkickID:".$songkickid;
  }
  return  "Post: ".$title." songkickID:none";
}

Cycling through all the posts

The above code updates a given post, so to update all posts I used the following:


function set_songkickid_all_posts()
{
  global $post;
  $args = array( 'numberposts' => -1, 'orderby'=> 'ID' );
  $posts = get_posts('numberposts=-1');

  foreach($posts as $post) : setup_postdata($post);
    //check if custom field has not been set
    $songkickid = get_post_meta($post->ID,'songkickid', true);
    if($songkickid == '')
    {
      $result = set_songkickid_from_title($post->ID);
      echo '<br />'.$result;
    }
  endforeach;
}

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.


require_once( dirname(__FILE__) . '/wp-load.php' );
require_once( dirname(__FILE__) . '/wp-admin/admin-functions.php' );

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.


<?php
require_once( dirname(__FILE__) . '/wp-load.php' );
require_once( dirname(__FILE__) . '/wp-admin/admin-functions.php' );

set_songkickid_all_posts();

function set_songkickid_all_posts()
{
  global $post;
  $args = array( 'numberposts' => -1, 'orderby'=> 'ID' );
  $posts = get_posts('numberposts=-1');

  foreach($posts as $post) : setup_postdata($post);
    //check if custom field has not been set
    $songkickid = get_post_meta($post->ID,'songkickid', true);
    if($songkickid == '')
    {
      $result = set_songkickid_from_title($post->ID);
      echo '<br />'.$result;
    }
  endforeach;
}

function set_songkickid_from_title($post_ID)
{
  $title = get_the_title($post_ID);
  $urltitle = urlencode($title);

  //build the url request
  $jsonurl = 'http://api.songkick.com/api/3.0/search/artists.json?query='.$urltitle.'&apikey=[your developer key]';

  $json = file_get_contents($jsonurl);
  $jsonarr = json_decode($json,true); //as an associative array

  $resultsarr = $jsonarr["resultsPage"]["results"];

  //check if any results returned
  if(count($resultsarr) > 0)
  {
    $songkickid = $resultsarr["artist"][0]["id"];
    add_post_meta($post_ID,'songkickid',$songkickid);
    return "Post: ".$title." songkickID:".$songkickid;
  }
  return  "Post: ".$title." songkickID:none";
}

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.


<?php 
if(is_single())
{
global $post;
$songkickid = get_post_meta($post->ID,'songkickid', true);
$output = do_shortcode('[songkick_concerts_and_festivals songkick_id='.$songkickid.' songkick_id_type=artist]'); 
echo $output;
}
else {
echo "Browse to a DJ to get their tour dates";
}
?>

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/

Leave a Response