Wordpress json rest API for getting user field data

I am currently using the JSON REST API (WP API) plugin to receive my posts and page data.

I noticed that none of my user field data is returned in json, and looking at the routes, I don't think I can get them.

Any ideas through the current plugin, or how can I do it otherwise?

+4
source share
4 answers

If you use "additional custom fields" - until a decision is made on a more official one, you can use this plugin: https://github.com/times/acf-to-wp-api (and now on a shelf in the standard area of ​​the plugin wp too.)

It will include custom fields under acf: [],in your json structure.

+1
source

You need to create this file containing the following code in

sor content \ Themes \ name \ on \ functions

   <?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

sor content \ Themes \ name \ on \ functions

to do this you must require this file for main.php

require_once dirname( __FILE__ ) . '/filename.php';
+1
source

JSON. , / , .

// 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 );
0
source

To get your own field value using only WP native functions, 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 the 'my_custom_field_key'field fields with your custom 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 );
0
source

Source: https://habr.com/ru/post/1568323/


All Articles