WordPress.org

Ready to get started?Download WordPress

Forums

Add data from xml to content (2 posts)

  1. jocken
    Member
    Posted 3 years ago #

    I'm trying to connect to last.fm to get artistinfo data directly to my custom post types, it works sometimes, but I have no idea when or how.

    I have defined the API in the beginning of the plugin. The problem seems to be when this should be executed.

    After I create the title of my custom post type (band) it should add data from last.fm and save to database when I publish it.

    function get_lastfm_artistinfo(){
    	global $user_ID;
    	$my_id = $post->ID;
    	$post_id_7 = get_post($my_id);
    	$title = $post_id_7->post_title;
    //Here is it where it goes wrong, sometimes it works and sometimes it doesn't.
    	if(get_post_type() == 'band' && $title != ''){
    		$band_link = str_replace('','%20',$title);
    		$band = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='.$title.'&api_key='. API_KEY;
    		$xml2 = simplexml_load_file($band);
    		foreach($xml2->children() as $first){
    			$new_post = array(
    			'post_title' => $title,
    			'post_content' => $first->bio->content,
    			'post_status' => 'publish',
    			'post_date' => date('Y-m-d H:i:s'),
    			'post_author' => $user_ID,
    			'post_type' => 'band',
    			'post_category' => array(0)
    			);
    			$post_id = wp_insert_post($new_post);
    		}
    	}
    
    }
    add_filter('the_content', 'get_lastfm_artistinfo');

    Does anyone had a similar issue?

  2. jocken
    Member
    Posted 3 years ago #

    Changed from wp_insert_post to wp_update_post, works almost right

    function get_lastfm_artistinfo(){
    	global $user_ID;
    	$my_id = $post->ID;
    	$post_id_7 = get_post($my_id);
    	$title = $post_id_7->post_title;
    	if(get_post_type() == 'band' && $title != ''){
    		$band_link = str_replace('','%20',$title);
    		$band = 'http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist='.$title.'&api_key='. API_KEY;
    		//Hämtar ut xml-data
    		$xml2 = simplexml_load_file($band);
    		foreach($xml2->children() as $first){
    			$new_post = array(
    			'post_content' => $first->bio->content,
    			'post_status' => 'publish',
    			'post_author' => $user_ID,
    			'post_type' => 'band',
    			'post_category' => array(0)
    			);
    			$post_id = wp_update_post($new_post);
    		}
    	}
    
    }
    
    	add_filter('the_content', 'get_lastfm_artistinfo');

    Still having problem with the filter, I have to comment and uncomment it to work.

Topic Closed

This topic has been closed to new replies.

About this Topic