Created
January 12, 2012 13:50
-
-
Save englebip/1600619 to your computer and use it in GitHub Desktop.
JSON WordPress feed
This file contains 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
<?php | |
/* | |
* JSON feed | |
* adapted from http://www.noeltock.com/web-design/wordpress/events-custom-post-types-fullcalendar-json-wordpress/ | |
*/ | |
function event_json_feed() { | |
header('Content-Type:application/json'); | |
global $wpdb; | |
// - grab date barrier - | |
$today = strftime('%F %R', strtotime('today 0:00')); | |
// - query - | |
global $wpdb; | |
$querystr = " | |
SELECT * | |
FROM $wpdb->posts wposts, $wpdb->postmeta metastart, $wpdb->postmeta metaend | |
WHERE (wposts.ID = metastart.post_id AND wposts.ID = metaend.post_id) | |
AND (metaend.meta_key = '_end_timestamp' AND metaend.meta_value > '$today' ) | |
AND metastart.meta_key = '_start_timestamp' | |
AND wposts.post_type = 'event' | |
AND wposts.post_status = 'publish' | |
ORDER BY metastart.meta_value ASC LIMIT 500 | |
"; | |
$events = $wpdb->get_results($querystr, OBJECT); | |
$jsonevents = array(); | |
// - loop - | |
if ($events) { | |
global $post; | |
foreach ($events as $post) { | |
setup_postdata($post); | |
// - custom post type variables - | |
$custom = get_post_custom(get_the_ID()); | |
$start = $custom["_start_timestamp"][0]; | |
$end = $custom["_end_timestamp"][0]; | |
// - grab gmt for start - | |
$gmts = date('Y-m-d H:i:s', strtotime($start)); | |
$gmts = get_gmt_from_date($gmts); // this function requires Y-m-d H:i:s | |
$gmts = strtotime($gmts); | |
// - grab gmt for end - | |
$gmte = date('Y-m-d H:i:s', strtotime($end)); | |
$gmte = get_gmt_from_date($gmte); // this function requires Y-m-d H:i:s | |
$gmte = strtotime($gmte); | |
// - set to ISO 8601 date format - | |
$stime = date('c', $gmts); | |
$etime = date('c', $gmte); | |
// - json items - | |
$jsonevents[]= array( | |
'title' => $post->post_title, | |
'allDay' => false, // <- true by default with FullCalendar | |
'start' => $stime, | |
'end' => $etime, | |
'url' => get_permalink($post->ID) | |
); | |
} | |
} | |
// - fire away - | |
echo json_encode($jsonevents); | |
} | |
add_feed('json', 'event_json_feed'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment