2

I'm trying to get the category name instead of the ID from the WP REST API for my Custom Post Type. Some articles about endpoint modification gave me some ideas on how to solve it, but unfortunately i'm not getting it to work. This is my Code (* removed irrelevant code at some lines):

CUSTOM POST TYPE

<?php
  add_action( 'init', 'portfolio_projects' );
        function portfolio_projects() {
          $labels = array([...]);

          $args = array(
             [...]
              'show_in_rest'          => true,
              'rest_base'             => 'projects',
              'rest_controller_class' => 'Category_Data',
              'supports'              => array( 'title', 'thumbnail', 'editor'),
              'taxonomies'            => array('post_tag', 'category')
          );

          register_post_type( 'project', $args );
      }

CONTROLLER CLASS

<?php
/**
 * Category data
 */
class Category_Data extends WP_REST_Posts_Controller
{

    public function init()
    {
        add_action('rest_api_init', array(
            $this,
            'add_category_data'
        ));
    }

    /**
     * Add the category data
     */
    public function add_category_data()
    {
        register_rest_field('project', 'category_data', ['get_callback' => array(
            $this,
            'get_all_category_data'
        ) , ]);
    }

    /**
     * Get all the category data
     *
     * @param $object
     * @param $field_name
     * @param $request
     *
     * @return array
     */
    public function get_all_category_data($object, $field_name, $request)
    {
        return get_the_category($object['id']);
    }
}

I'd love to hear your ideas and thoughts on this. Thanks

2
  • edit your question to add the code you try to add a endpoint.
    – mmm
    Dec 6, 2017 at 18:41
  • i'm not sure what you mean by that. This is all i have in my functions.php
    – moeses
    Dec 6, 2017 at 19:33

2 Answers 2

6

This code will add categories_names field to wp rest api response:

function wpse_287931_register_categories_names_field() {

    register_rest_field( 'project',
        'categories_names',
        array(
            'get_callback'    => 'wpse_287931_get_categories_names',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}

add_action( 'rest_api_init', 'wpse_287931_register_categories_names_field' );

function wpse_287931_get_categories_names( $object, $field_name, $request ) {

    $formatted_categories = array();

    $categories = get_the_category( $object['id'] );

    foreach ($categories as $category) {
        $formatted_categories[] = $category->name;
    }

    return $formatted_categories;
}
2
  • 1
    thanks that did the job! I'd like to know what was wrong about my approach
    – moeses
    Dec 6, 2017 at 21:29
  • 1
    Your code is not executing. I don't know from where you get this but looking at your code your are assuming that init method will be executed but is not.
    – kierzniak
    Dec 6, 2017 at 21:52
4

The same as above but replacing 'project' for array('post') should work.

function wpse_287931_register_categories_names_field()
{
    register_rest_field(
        array('post'),
        'categories_names',
        array(
            'get_callback'    => 'wpse_287931_get_categories_names',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}

add_action('rest_api_init', 'wpse_287931_register_categories_names_field');

function wpse_287931_get_categories_names($object, $field_name, $request)
{
    $formatted_categories = array();

    $categories = get_the_category($object['id']);

    foreach ($categories as $category) {
        $formatted_categories[] = $category->name;
    }

    return $formatted_categories;
}

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.