Skip to content

Instantly share code, notes, and snippets.

@LinzardMac
Last active December 11, 2015 22:29
Show Gist options
  • Save LinzardMac/4670017 to your computer and use it in GitHub Desktop.
Save LinzardMac/4670017 to your computer and use it in GitHub Desktop.
$tweetq= pods('twitter_resources');
$tweetq->find();
$response = array();
#This looks for every pods record and gets fields for each of them and puts them into a url for each record
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) {
# Loop through each url and parse each url
foreach( $response as $response->results){
// Parse the response and grab the max_id
$parsed_reponse = json_decode(wp_remote_retrieve_body($response));
if(!empty($parsed_reponse->results)) {
## Now, after we've already used the empty variable $max_id
$max_id = $parsed_reponse->max_id_str;
update_option('ttp_max_tweet_id', $max_id);
}
# Loop through the tweets (search results) and publish posts
foreach($parsed_reponse->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