Created
January 30, 2013 02:05
-
-
Save LinzardMac/4669970 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$tweetq= pods('twitter_resources'); | |
$tweetq->find(); | |
while ( $tweetq->fetch() ) : | |
$fromname = $tweetq->field('from_user'); | |
# $hashtag = $tweetq->field('hash_tag'); not needed anymore | |
$categorize = $tweetq->field('categorize'); | |
# Get only recent strings from twitter to avoid pulling dupes | |
$max_id = get_option('ttp_max_tweet_id', 0); | |
// Fire the HTTP request to Twitter | |
$response = wp_remote_get('http://search.twitter.com/search.json?q=from:'. urlencode($fromname) .'&result_type=recent&rpp=10&since_id='.$max_id); | |
endwhile; | |
// Make sure no errors occured | |
if(!is_wp_error($response) && wp_remote_retrieve_response_code($response) == 200) { | |
// Parse the response and grab the max_id | |
$response = json_decode(wp_remote_retrieve_body($response)); | |
if(!empty($response->results)) { | |
//loop through each respose url and pull results for each | |
foreach($response->results as $response){ | |
## Now, after we've already used the empty variable $max_id | |
$max_id = $response->max_id_str; | |
update_option('ttp_max_tweet_id', $max_id); | |
} | |
// Loop through the tweets (search results) and publish posts | |
foreach($response->results as $tweet) { | |
// Parse the tweet's timestamp | |
$gmt_date = date('Y-m-d H:i:s', strtotime($tweet->created_at)); | |
$post = array( | |
'post_content' => $tweet->text, | |
'post_type' => 'imported_content', | |
'post_title' => __('Tweet', 'ttp').'-'.$tweet->id, | |
'post_status' => 'publish', | |
'post_date_gmt' => $gmt_date, | |
'post_date' => get_date_from_gmt($gmt_date), | |
'post_author' => $author->ID, | |
#'tax_input' => array('categories' => array('foo')) // Support for custom taxonomies. | |
); | |
$post_id = wp_insert_post($post); | |
set_post_format($post_id , 'quote'); | |
wp_set_post_terms( $post_id, '4', 'category', false ) ; | |
update_post_meta( $post_id, 'tweet_author_avatar', $tweet->profile_image_url_https); | |
update_post_meta( $post_id, 'tweet_author', $tweet->from_user); | |
update_post_meta( $post_id, 'tweet_url', $tweet->id_str); | |
} | |
} | |
echo '<h1>Your tweets have been imported</h1>'; | |
} | |
else { | |
echo '<h1>Oh no! There has been an error in accessing your twitter feed. </h1>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment