I am customising the Wordpress plugin, JSON API. I want to output:
Display all published today's posts for each category in one big JSON tree.
IE:
Concept output:
Category 1
Post 1
Post 2
Category 2
No posts
Category 3
Post 1
Post 2
Post 3
I am following the tutorial about grabbing the latest posts by category however this seems to indicate I have to do 2 loops, one to grab all the categories and then another to grab each post within that category.
My custom function currently grabs the categories fine, but I am unsure of how to combine the two loops into a JSON tree using the current JSON API plugin.
// This is in the introspector.php file
public function get_todays_posts($today)
{
global $wpdb;
/* // Show posts for category 5 - this works, but it doesn't show the category name
$q = query_posts('cat=5&year='.$today["year"].'&monthnum='.$today["month"].'&day=' .$today["day"].'&showposts=-1&orderby=post_date&order=desc');
*/
// output for this is below
$sql = "SELECT * FROM gc_terms AS wterms INNER JOIN gc_term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category'";
$data = $wpdb->get_results(($sql));
$results = array();
foreach ($data as $row)
{
$results[] = new JSON_API_Category($row);
}
return($results);
} // end function
My output is:
{
"status": "ok",
"count": 17,
"count_total": 4,
"pages": 1,
"posts": [
{
"id": 1,
"slug": "general",
"title": "General",
"description": "",
"parent": 0,
"post_count": 0
}
// etc
}
Ideally I would like to output all of todays posts under each category, but the problem that I am facing is how to do two loops and combine them into the JSON API plugin.
Any help or guidance on this would be most appreciated.
Thanks.
EDIT:
I have found a method which I have customized which gets me a step closer
global $json_api;
$category = $json_api->introspector->get_category_by_id(5);
if (!$category) {
$json_api->error("Not found.");
}
$posts = $json_api->introspector->get_posts(array(
'cat' => $category->id,
'post_status' => 'publish',
'year' => 2011,
'monthnum' => 10,
'day' => 01,
'orderby' => 'post_date',
'order' => 'ASC'
));
return $this->posts_object_result($posts, $category);
However, this is in-correct because posts are not within the category as seen in this screenshot from JSONPad.
I understand that I need to loop through the outer categories and then its children to get the output I want; however the problem is making json-api understand how to combine arrays into a valid JSON tree.
I have decided not to resolve this problem by getting the categories from the JSON API from my app and then getting the posts relevant to my category as-and-when I need them.
Its bit of a long-way round, but it will do for now.