0

I want help on WP REST API I want to display Categories with a first level child with some attributes. Here how it should look like:

{
  Parent
  "id": 3,
  "name": "Football",
  "slug": "football"
  child {
            "id": 5,
            "name": "League",
            "slug": "league"
    }
}

Please, anyone help me... Thanks

2
  • I think you should elaborate your question a little more. Have you tried anything, is there any code you tried to use, etc? meanwhile take a look at register_rest_field function and rest_prepare_post_type and rest_api_init hooks. Oct 19 '17 at 20:57
  • oh! and what endpoint are you targeting? You want to achieve that on taxonomy endpoint or want to get this info in each item of a post_type endpoint? Oct 19 '17 at 21:05
0

I am assuming you want these taxonomies data in each item of post_type JSON, but the logic is the same for taxonomies endpoint and JSON response.

I see two ways of achieving it.

  1. filtering the data using rest_prepare_post_type filter (Reference) or
  2. creating new field(s) using the register_rest_field function. (Reference)

I would recommend the second approach. This is the one I've used recently with a custom post type

Case 1: rest_prepare_post_type

In the first case, you would use the rest_prepare_post_type filter to get the data, generate the data you want, add it to the data array and return the filtered data. In this case, if the post type is "post", the hook is rest_prepare_post.

/**
 * @summary    Filters post rest response
 *
 * @description    Filters post rest response removing or adding certain information
 * @param     object    $data       The response object.
 * @param     object    $post       The original post type object.
 * @param     object    $request    Request used to generate the response.
 * @return    object    $data       The filtered response object.
 */
function wpse_283452_rest_prepare_post($data, $post, $request) {
    $_data = $data->data;
    // say you want to remove author from the response
    unset($_data['author']);
    // if you want to ADD fields, you would do the logic here and...
    // add it to the $_data array
    $data->data = $_data;
    // and finally return it
    return $data;
}
add_filter('rest_prepare_post', 'wpse_283452_rest_prepare_post', 10, 3);

If you want to the same for taxonomies, you would use rest_prepare_taxonomy/ (reference)

Case 2: register_rest_field (recommended)

You will register a new field with register_rest_field function and populate it the way you want using a callback

/**
 * Registers a new field on an existing WordPress object type.
 *
 * @since 4.7.0
 *
 * @global array $wp_rest_additional_fields Holds registered fields, organized
 *                                          by object type.
 *
 * @param string|array $object_type Object(s) the field is being registered
 *                                  to, "post"|"term"|"comment" etc.
 * @param string $attribute         The attribute name.
 * @param array  $args {
 *     Optional. An array of arguments used to handle the registered field.
 *
 *     @type string|array|null $get_callback    Optional. The callback function used to retrieve the field
 *                                              value. Default is 'null', the field will not be returned in
 *                                              the response.
 *     @type string|array|null $update_callback Optional. The callback function used to set and update the
 *                                              field value. Default is 'null', the value cannot be set or
 *                                              updated.
 *     @type string|array|null $schema          Optional. The callback function used to create the schema for
 *                                              this field. Default is 'null', no schema entry will be returned.
 * }
 */
register_rest_field(
    'post',
    'wpse_283452_taxonomies',
    array(
        'get_callback' => function ($object, $field_name, $request) {
            return wpse_283452_taxonomies_callback($object);
        },
    )
);

/**
 * @summary    Return and array with taxonomy info
 *
 * @description    Returns an array with taxonomy information in a custom way
 * @param     object    $object            The rest item original object
 * @return    array     $custom_taxonom    an array with taxonomy terms with their information in a custom way
 */
function wpse_283452_taxonomies_callback($object) {
    $post_id = $object['id'];
    $terms = wp_get_post_terms(
        $post_id,
        'business_type',
        array(
            'fields' => 'all',
        )
    );
    // here you should construct an array with the information in $terms in the way/structure you want
    return $terms;
}

For taxonomies, you would use the taxonomy slug instead of 'post' as the 1st argument on register_rest_field.

That's it.

Let me know if works.

2
  • Hey, can tell me how to display all categories with custom endpoint
    – Sujit
    Oct 24 '17 at 6:12
  • sujit the point of Stackexchange is to help people develop their own skills, not giving the full answer. You should, at least, try to code your own solution based on what users and then asking for clarification on specific points or roadblocks you find. Oct 24 '17 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.