Skip to content

Instantly share code, notes, and snippets.

@kkestell
Created July 8, 2011 18:42
Show Gist options
  • Save kkestell/1072489 to your computer and use it in GitHub Desktop.
Save kkestell/1072489 to your computer and use it in GitHub Desktop.
Get WP Posts as JSON
<?php
require_once(dirname(__FILE__) . '/wp-load.php');
require_once(dirname(__FILE__) . '/wp-includes/post.php');
$args = array(
'numberposts' => 100,
'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish'
);
$posts = get_posts($args);
$items = array();
foreach($posts as $post) {
setup_postdata($post);
$item = array();
$item['guid'] = $post->guid;
$item['title'] = $post->post_title;
$item['content'] = $post->post_excerpt;
$tid = get_post_thumbnail_id($post->ID);
$thumb = get_post($tid);
$item['thumbnail'] = $thumb->guid;
if($item['guid'] != $item['thumbnail']) {
$items[] = $item;
}
if(count($items) == 3) break;
}
echo json_encode($items);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment