Jürgen Gunz
Jürgen Gunz

Reputation: 105

Wordpress Rest API: How to query Category Name by id in Angularjs

I am working on my first app, based on Ionic and Angularjs connected to Wordpress REST Api.

I need to display the category name, but the WP-API V2 post list (example.com/wp-json/wp/v2/posts) has only the category ids.

In order to get the categories I need to make a second http request to example.com/wp-json/wp/v2/categories

This is my function in the controller to load all posts and it works fine.

 var postsApi = $rootScope.url + 'posts';      
$scope.loadPosts = function() {

// Get all of our posts
DataLoader.get( postsApi ).then(function(response) {

  $scope.posts = response.data;

  $log.log(postsApi, response.data);


}, function(response) {
  $log.log(postsApi, response.data);
});

but how do I achieve to parse the category id, that i get from example.com/wp-json/wp/v2/posts with example.com/wp-json/wp/v2/categories to get the category name without making a http request every single loop?

Upvotes: 0

Views: 3740

Answers (2)

Benoti
Benoti

Reputation: 2210

You maybe need to use register_new_field() to modify the response from the api.

Modify response from wp-api v2

In your custom function, you will able to retrieve the post categories in one api call and embed it in the json response.

EDIT:

Here is a working example, to add post category and tag link to the api response, only for the get request:

add_action( 'rest_api_init', 'wp_rest_insert_tag_links' );

function wp_rest_insert_tag_links(){

    register_rest_field( 'post',
        'post_categories',
        array(
            'get_callback'    => 'wp_rest_get_categories_links',
            'update_callback' => null,
            'schema'          => null,
        )
    );
    register_rest_field( 'post',
        'post_tags',
        array(
            'get_callback'    => 'wp_rest_get_tags_links',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}

function wp_rest_get_categories_links($post){
    $post_categories = array();
    $categories = wp_get_post_terms( $post['id'], 'category', array('fields'=>'all') );

foreach ($categories as $term) {
    $term_link = get_term_link($term);
    if ( is_wp_error( $term_link ) ) {
        continue;
    }
    $post_categories[] = array('term_id'=>$term->term_id, 'name'=>$term->name, 'link'=>$term_link);
}
    return $post_categories;

}
function wp_rest_get_tags_links($post){
    $post_tags = array();
    $tags = wp_get_post_terms( $post['id'], 'post_tag', array('fields'=>'all') );
    foreach ($tags as $term) {
        $term_link = get_term_link($term);
        if ( is_wp_error( $term_link ) ) {
            continue;
        }
        $post_tags[] = array('term_id'=>$term->term_id, 'name'=>$term->name, 'link'=>$term_link);
    }
    return $post_tags;
}

Upvotes: 2

R.Villanueva
R.Villanueva

Reputation: 130

I believe that your only way is to change your API in a way that it returns in the first call everything you need and not only the ids, or create a new service in the API.

If you send an id and recieve the data of the post is not a way of doing that to various post in a single call.

Upvotes: 0

Related Questions