Title: WP Rest API v2 access
Last modified: February 27, 2017

---

# WP Rest API v2 access

 *  Resolved [drsdre](https://wordpress.org/support/users/drsdre/)
 * (@drsdre)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/wp-rest-api-access-2/)
 * Now that the WP Rest API v2 has become part of core, I’m trying to get programmatically
   access to the thirstylink records. I’ve used the Pods plugin to extend the thristylink
   post type and enabled the Rest API v2 on it.
 * Unfortunately not all the information is being provided, in particular the title
   field information is missing. This most likely has something to do with the the
   setup this custom post type where title and other fields are disabled (see [https://plugins.svn.wordpress.org/thirstyaffiliates/trunk/thirstyaffiliates.php](https://plugins.svn.wordpress.org/thirstyaffiliates/trunk/thirstyaffiliates.php)):
 *     ```
       register_post_type(
       		'thirstylink',
       		array(
       			'labels' => array(
       				'name' => __('Affiliate Links', 'thirstyaffiliates'),
       				'singular_name' => __('Affiliate Link', 'thirstyaffiliates'),
       				'add_new_item' => __('Add New Affiliate Link', 'thirstyaffiliates'),
       				'edit_item' => __('Edit Affiliate Link', 'thirstyaffiliates'),
       				'view_item' => __('View Affiliate Link', 'thirstyaffiliates'),
       				'search_items' =>  __('Search Affiliate Links', 'thirstyaffiliates'),
       				'not_found' => __('No Affiliate Links found!', 'thirstyaffiliates'),
       				'not_found_in_trash' => __('No Affiliate Links found in trash', 'thirstyaffiliates'),
       				'menu_name' => __('Affiliate Links', 'thirstyaffiliates'),
       				'all_items' => __('All Affiliate Links', 'thirstyaffiliates')
       			),
       			'description' => __('ThirstyAffiliates affiliate links', 'thirstyaffiliates'),
       			'public' => true,
       			'menu_position' => 20,
       			'hierarchical' => true,
       			'supports' => array(
       				'title' => false,
       				'editor' => false,
       				'author' => false,
       				'thumbnail' => false,
       				'excerpt' => false,
       				'trackbacks' => false,
       				'comments' => false,
       				'revisions' => false,
       				'page-attributes' => false,
       				'post-formats' => false
       			),
       			'show_in_menu' => true,
       			'show_in_nav_menus' => true,
       			'can_export' => true,
       			'has_archive' => false,
       			'rewrite' => array(
       				'slug' => $slug,
       				'with_front' => false,
       				'pages' => false
       			),
       			'menu_icon' => plugins_url('thirstyaffiliates/images/icon-aff.png'),
       			'exclude_from_search' => true
       		)
       	); 
       ```
   
 * Any ideas on how to circumvent this issue or alternative ways to enable the data
   in WP Rest API v2?
    -  This topic was modified 9 years, 3 months ago by [drsdre](https://wordpress.org/support/users/drsdre/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [drsdre](https://wordpress.org/support/users/drsdre/)
 * (@drsdre)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/wp-rest-api-access-2/#post-8853048)
 * After some fiddling around, the code below (to be used as a plugin) is doing 
   the job for me. If you implement this, you probably want to add a setting to 
   enable it explicitly and also split the thirstyData field into specific fields.
 *     ```
       <?php
       /**
        * Plugin Name:     ThirstyAffiliates API
        * Plugin URI:      https://github.com/drsdre
        * Description:     Enable WP Core Rest API v2 for ThirstyAffiliates data
        * Author:          Andre Schuurman
        * Author URI:      https://github.com/drsdre
        * Text Domain:     thirstyaffiliates-api
        * Domain Path:     /languages
        * Version:         0.1.0
        *
        * @package         thirstyaffiliates-api
        */
   
       /**
        * Add REST API support to thirstylink post type.
        */
       add_action( 'init', 'thirstylink_post_enable_rest_api', 25 );
       function thirstylink_post_enable_rest_api() {
       	global $wp_post_types;
   
       	$post_type_name = 'thirstylink';
       	if ( isset( $wp_post_types[ $post_type_name ] ) ) {
       		$wp_post_types[ $post_type_name ]->show_in_rest = true;
       		$wp_post_types[ $post_type_name ]->rest_base    = $post_type_name;
       		$wp_post_types[ $post_type_name ]->supports['custom-fields'] = true;
   
       	}
       }
   
       /**
        * Add REST API support to thirstylink-category taxonomy type.
        */
       add_action( 'init', 'thirstylink_category_enable_rest_api', 25 );
       function thirstylink_category_enable_rest_api() {
       	global $wp_taxonomies;
   
       	$taxonomy_name = 'thirstylink_category';
       	if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
       		$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
       		$wp_taxonomies[ $taxonomy_name ]->rest_base    = $taxonomy_name;
   
       	}
       }
   
       /**
        * Add REST API support to thirstylink title and thirstyData fields.
        */
       add_action( 'rest_api_init', function () {
       	register_rest_field( 'thirstylink', 'title', [
       		'get_callback'    => function ( $thirstylink_data ) {
       			$thirstylink_obj = get_post( $thirstylink_data['id'] );
   
       			return $thirstylink_obj->post_title;
       		},
       		'update_callback' => function ( $title, $thirstylink_obj ) {
       			$ret = wp_update_post( [
       				'id'         => $thirstylink_obj->ID,
       				'post_title' => $title,
       			] );
       			if ( false === $ret ) {
       				return new WP_Error( 'rest_thirstylink_title_failed', __( 'Failed to update title.' ),
       					[ 'status' => 500 ] );
       			}
   
       			return true;
       		},
       		'schema'          => [
       			'description' => __( 'Thirstylink title.' ),
       			'type'        => 'string',
       		],
       	] );
   
       	register_rest_field( 'thirstylink', 'thirstyData', [
       		'get_callback'    => function ( $thirstylink_data ) {
       			$thirstylink_data_obj = get_post_meta( $thirstylink_data['id'], 'thirstyData', true );
   
       			return $thirstylink_data_obj;
       		},
       		'update_callback' => function ( $data, $thirstylink_obj ) {
       			$ret = update_post_meta( $thirstylink_obj->ID, 'thirstyData', $data);
   
       			if ( false === $ret ) {
       				return new WP_Error( 'rest_thirstylink_data_failed', __( 'Failed to update data.' ),
       					[ 'status' => 500 ] );
       			}
   
       			return true;
       		},
       		'schema'          => [
       			'description' => __( 'Thirstylink data.' ),
       			'type'        => 'string',
       		],
       	] );
       } );
       ```
   
 *  Thread Starter [drsdre](https://wordpress.org/support/users/drsdre/)
 * (@drsdre)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/wp-rest-api-access-2/#post-8853146)
 * For convenience, this plugin is now also available on GitHub [https://github.com/drsdre/thirstyaffiliates-api](https://github.com/drsdre/thirstyaffiliates-api)
 *  [Josh Kohlbach](https://wordpress.org/support/users/jkohlbach/)
 * (@jkohlbach)
 * [9 years, 3 months ago](https://wordpress.org/support/topic/wp-rest-api-access-2/#post-8859266)
 * Brilliant work [@drsdre](https://wordpress.org/support/users/drsdre/)!
 * We’ll look at rolling this into core in the future if you’re happy for us to 
   do that (with proper attribution of course).
 * Cheers,
    Josh

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘WP Rest API v2 access’ is closed to new replies.

 * ![](https://ps.w.org/thirstyaffiliates/assets/icon-256x256.jpg?rev=2686825)
 * [ThirstyAffiliates - Affiliate Links, Link Branding, Link Tracking & Marketing Plugin](https://wordpress.org/plugins/thirstyaffiliates/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/thirstyaffiliates/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/thirstyaffiliates/)
 * [Active Topics](https://wordpress.org/support/plugin/thirstyaffiliates/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/thirstyaffiliates/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/thirstyaffiliates/reviews/)

## Tags

 * [api](https://wordpress.org/support/topic-tag/api/)
 * [post-type](https://wordpress.org/support/topic-tag/post-type/)
 * [rest](https://wordpress.org/support/topic-tag/rest/)
 * [v2](https://wordpress.org/support/topic-tag/v2/)

 * 3 replies
 * 2 participants
 * Last reply from: [Josh Kohlbach](https://wordpress.org/support/users/jkohlbach/)
 * Last activity: [9 years, 3 months ago](https://wordpress.org/support/topic/wp-rest-api-access-2/#post-8859266)
 * Status: resolved