WordPress.org

Ready to get started?Download WordPress

Forums

JSON API
Added Terms controller to fetch terms by taxonomy or post (6 posts)

  1. Einkoro
    Member
    Posted 11 months ago #

    Hi,

    I ran into a project where I needed to be able to access terms from custom taxonomies on the client side so I forked JSON API on github and added a Terms controller with support for: get_terms(), get_term() and get_the_terms()

    I also added support for term images on custom taxonomies if the Taxonomy Images plugin is installed and activated.

    Full change notes are available in readme.md from:
    https://github.com/Einkoro/wp-json-api

    The terms controller's source is here:
    https://github.com/Einkoro/wp-json-api/blob/master/controllers/terms.php

    http://wordpress.org/extend/plugins/json-api/

  2. alexaung
    Member
    Posted 11 months ago #

    Hi,

    Thanks for the add on.

    Could You please add the function to get the taxonomies by parent?

    e.g parent=0 or parent=1

    Thanks,
    Alex

  3. Einkoro
    Member
    Posted 11 months ago #

    You can use get_terms to fetch the children of a term for categories like this:
    /api/terms/get_terms/?taxonomy=category&term_args[parent]=my_parent_term_id

    If you want all children of a term (sub categories of sub categories) rather than direct children you can use this:
    /api/terms/get_terms/?taxonomy=category&term_args[child]=my_parent_term_id

    To fetch the parent of a term you can use get_term twice:
    /api/terms/get_term/?term_id=my_term_id&taxonomy=category

    Then with the parent from the previous term's data you can call it again:
    /api/terms/get_term/?term_id=my_parent_term_id&taxonomy=category

    I could add a shortcut such as get_term_parent to only do one call for parents even though there is no equivalent in WordPress's API if you'd like.

  4. alexaung
    Member
    Posted 11 months ago #

    Hi Einkoro,

    Thanks for reply. Yes, I got it but there is some problem. I have 16 categories but it only return 3 categories. I

    http://www.myhost.com/api/terms/get_terms/?taxonomy=ad_cat&term_argsparent=0

    Another example
    I have 12 sub categories but it is only return 2. Yes I only have post for those sub categories.
    http://www.myhost.com/api/terms/get_terms/?taxonomy=ad_cat&term_argsparent=18

    Regards,
    Alex

  5. Einkoro
    Member
    Posted 11 months ago #

    If you add &term_args[hide_empty]=0 do you get the expected number of categories? By default get_terms will hide any terms without posts associated with them.

  6. alexaung
    Member
    Posted 11 months ago #

    Thanks Einkoro,

    I got the expected result. Great Add On. Save alot of my time.

Reply

You must log in to post.

About this Plugin

About this Topic