sovanlandy
sovanlandy

Reputation: 1700

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

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

Answers (8)

celsowm
celsowm

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

Crawler
Crawler

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:

  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.

Upvotes: 3

crifan
crifan

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:

  1. install wordpress plugin: JSON API wordpress plugin JSON API
  2. use 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

itinance
itinance

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

Marcelo Ribeiro
Marcelo Ribeiro

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

Jason Lydon
Jason Lydon

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

Miguel Valencia
Miguel Valencia

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

Rajika Nanayakkara
Rajika Nanayakkara

Reputation: 623

try this <?php echo get_the_category_list(); ?>

Upvotes: -3

Related Questions