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/
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With