wordpress json rest API to get custom field data

I am currently using the JSON REST API (WP API) plug-in to get my post and page data.

I have noticed that none of my custom field data is returned in the json, and looking at the routes, i don’t think i can obtain these.

Read More

Any ideas via the current plug-in, or how I can accomplish this otherwise?

Related posts

Leave a Reply

4 comments

  1. To grab a custom field value using native WP functions only, add the following to your functions.php

    function my_rest_prepare_post( $data, $post, $request ) {
      $_data = $data->data;
      $_data[$field] = get_post_meta( $post->ID, 'my_custom_field_key', true );
      $data->data = $_data;
      return $data;
    }
    add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 );
    

    Replace 'my_custom_field_key' with your custom field key name.

    For multiple fields:

    function my_rest_prepare_post( $data, $post, $request ) {
      $_data = $data->data;
      // My custom fields that I want to include in the WP API v2 responce
      $fields = ['writer', 'publisher', 'year', 'youtube_link'];
    
      foreach ( $fields as $field ) {
        $_data[$field] = get_post_meta( $post->ID, $field, true );
      }
    
      $data->data = $_data;
      return $data;
    }
    
    add_filter( 'rest_prepare_post', 'my_rest_prepare_post', 10, 3 );
    
  2. You need to create this file contain following code in

    wp-contentthemesnameincfunctions

       <?php
            if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
            /*
             * init function
             */
            if ( ! function_exists( 'mnu_rest_init' ) ) {
                      function mnu_rest_init() {       
                          register_rest_route( 'guider/v1', '/booking', array(
                          'methods' => 'GET',
                          'callback' => 'handle_get_all',
                          'permission_callback' => function () {
                           return current_user_can( 'edit_others_posts' );
                        }
                      ) );
                    register_rest_route( 'guider/v1', '/booking', array(
                          'methods' => 'POST',
                          'callback' => 'handle_post_booking',
                          'permission_callback' => function () {
                           return current_user_can( 'edit_others_posts' );
                        }
                      ) );
              }
            }
    
        //GET QUERY PARMS
        function handle_get_all( $request_data) {
            $parameters = $request_data->get_params();
             $userId = $parameters["Id"];
             global $wpdb;
             $query = "SELECT * FROM `wp_trav_tour_bookings` WHERE `user_id` = $userId";
             $list = $wpdb->get_results($query);
            return $list;
        }
    
        // GET BODY PARMS
        function handle_post_booking( $request_data) {
            $parameters = $request_data->get_body();
            $params = json_decode( $parameters , true );
           //  $userId = $parameters["Id"];
            // global $wpdb;
            // $query = "SELECT * FROM `wp_trav_tour_bookings` WHERE `user_id` = $userId";
            // $list = $wpdb->get_results($query);
            return $params ;
        }
    

    then you need to add

    //actions
    add_action( 'rest_api_init', 'mnu_rest_init');   
    

    to your main.php in

    wp-contentthemesnameincfunctions

    to do that you need to require this file to main.php

    require_once dirname( __FILE__ ) . '/filename.php';
    
  3. You can manipulate the response and add custom fields to the JSON. I’m using Advanced Custom Fields in my example but you can just add any key/value-pairs to the data object before returning it.

    // In functions.php
    function modify_rest_post( $data, $post, $request ) {
      if (is_admin()) {
        return $data;
      }
    
      $data->my_favorite_data = get_field('my_custom_field', $post->ID);
      return $data;
    }
    
    add_filter( 'rest_prepare_post', 'modify_rest_post', 10, 3 );