Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a list of categories/ tag in Wordpress REST API

Tags:

wordpress

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/

like image 341
sovanlandy Avatar asked Nov 23 '13 04:11

sovanlandy


3 Answers

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.

like image 154
Jason Lydon Avatar answered Oct 22 '22 15:10

Jason Lydon


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:

  • http://www.example.com/?json=1
  • http://www.example.com/?p=47&json=1
  • http://www.example.com/tag/banana/?json=1

2. Explicit mode

JSON query use known string value i.e "json=get_recent_post".Examples:

  • http://www.example.org/?json=get_recent_posts
  • http://www.example.org/?json=get_post&post_id=47
  • http://www.example.org/?json=get_tag_posts&tag_slug=banana

3. Permalink mode

No JSON query but user friendly permalinks are used for request i.e "/api/get_recent_post".Examples:

  • http://www.example.org/api/get_recent_posts/
  • http://www.example.org/api/get_post/?post_id=47
  • http://www.example.org/api/get_tag_posts/?tag_slug=banana

Also, syntax for listing category is:

http://blog.example.com/?json=get_category_index

Also other important basic request are:

  1. http://blog.example.com/?json=get_tag_index(To get list of Tag)
  2. http://blog.example.com/?json=get_author_index(To get list of Author)
  3. http://blog.example.com/?json=get_page_index(To get list of Page)
  4. http://blog.example.com/?json=get_date_index(To get list of Date)

More details can be found in this link. Hope this will save someone's time who is not from wordpress background like me.

like image 25
Crawler Avatar answered Oct 22 '22 16:10

Crawler


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.

like image 37
itinance Avatar answered Oct 22 '22 16:10

itinance