Reputation: 1700
Does anyone know how to get a list of categories in Wordpress JSON Rest API? It seems the current API does not support retrieve list of categories (While XML-RPC does).
http://developer.wordpress.com/docs/api/
Upvotes: 9
Views: 27251
Reputation: 414
it is necessary to paging the categories, for example:
https://example.com/wp-json/wp/v2/categories?page=2
and you can increase the number per page:
https://example.com/wp-json/wp/v2/categories?page=1&per_page=100
Upvotes: 2
Reputation: 2018
EDIT: As of August 2019, the JSON API plugin is not available due to security concerns.
Im was trying to make andriod app out of my wordpress site. Thanks to JSON API that was possible but got real headace. There was no any documentation for request syntax.
After 2 hrs of research, I finally dug something out. First thing to know, there are 3 types of request mode:
1. Implicit mode
JSON query use non empty value i.e "json=1".Examples:
2. Explicit mode
JSON query use known string value i.e "json=get_recent_post".Examples:
3. Permalink mode
No JSON query but user friendly permalinks are used for request i.e "/api/get_recent_post".Examples:
Also, syntax for listing category is:
http://blog.example.com/?json=get_category_index
Also other important basic request are:
More details can be found in this link. Hope this will save someone's time who is not from wordpress background like me.
Upvotes: 3
Reputation: 14338
EDIT: As of August 2019, The JSON API plugin is closed due to security reasons. This answer is therefore deprecated.
finally workable steps:
JSON API
get_category_index
api, format is: http://www.example.com/api/get_category_index
then can got response categories:
{
"status": "ok",
"count": 332,
"categories": [{
"id": 4637,
"slug": "soft_360",
"title": "360",
"description": "",
"parent": 4618,
"post_count": 2
}, {
"id": 4498,
"slug": "amazon",
"title": "Amazon",
"description": "",
"parent": 3390,
"post_count": 29
}, {
......
}, {
"id": 860,
"slug": "default_classification",
"title": "\u9ed8\u8ba4\u5206\u7c7b",
"description": "",
"parent": 17,
"post_count": 3
}]
}
more info can refer official document: JSON API — WordPress Plugins
Upvotes: 1
Reputation: 12428
With current versions of Wordpress 4.9.8 usually categories can be fetched with the builtin-API this way:
http://www.example.com/wp-json/wp/v2/categories
However, there seems to be a bug that will not fetch all categories, at least if you were using parent- and child-categories.
I added a small PHP-script to our wordpress-installation to retrieve ALL categories properly:
<?php
/** Make sure that the WordPress bootstrap has run before continuing. */
require( dirname(__FILE__) . '/wp-load.php' );
// Redirect to https login if forced to use SSL
if ( force_ssl_admin() && ! is_ssl() ) {
if ( 0 === strpos($_SERVER['REQUEST_URI'], 'http') ) {
wp_safe_redirect( set_url_scheme( $_SERVER['REQUEST_URI'], 'https' ) );
exit();
} else {
wp_safe_redirect( 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
exit();
}
}
header('Content-Type: application/json');
echo json_encode(get_categories());
Notice:
For testing purposes you should be aware that get_categories
-function will return only those that are already associated with at least one (published?) article.
Upvotes: 2
Reputation: 331
$args = [
'taxonomy' => 'category',
'hide_empty' => 0,
'parent' => 0
];
function _get_child_terms( $items ) {
foreach ( $items as $item ) {
$item->children = get_terms( 'category', array( 'child_of' => $item->term_id, 'hide_empty' => 0 ) );
if ( $item->children ) _get_child_terms( $item->children );
}
return $items;
}
$terms = _get_child_terms( get_terms( $args ) );
echo json_encode( $terms );
Upvotes: 0
Reputation: 7180
Per WP REST API page you could hit http://example.com/wp-json/wp/v2/categories. This may be an addition to v2 of the Rest API, but I'm not sure.
Upvotes: 7
Reputation: 221
If you're using the JSON API supplied by Jetpack, you may be out of luck. Looking at their API documentation (which they claim to be automatically updated), there is no reference to getting a list of categories. If it helps, though, you can get information about each individual category.
I just tried http://public-api.wordpress.com/rest/v1/sites/$site/categories and it returned nothing. Sorry.
http://developer.wordpress.com/docs/api/
Upvotes: 0