I'm trying to get my custom fields to show up during an WordPress rest API call. But I can't find the proper function to call to get it to work. (Or I'm doing it wrong).
What am I missing?
Is there any documentation that you are following?
I'm using this mostly: hidden link
Is there a similar example that we can see?
hidden link
What is the link to your site?
hidden link
Here's the custom plugin I created:
<?php
/**
* Plugin Name: Custom API
* Plugin URI: hidden link
* Description: Custom API for bat digest posts
* Version: 1.0
* Author: Me
* Author URI: hidden link
*/
function wl_bat_listings() {
$args = [
'numberposts' => 99999,
'post_type' => 'bat-listing'
];
$posts = get_posts($args);
$data = [];
$i = 0;
foreach($posts as $post) {
$data[$i]['id'] = $post->ID;
$data[$i]['title'] = $post->post_title;
$data[$i]['slug'] = $post->post_name;
$data[$i]['price'] = get_value('wpcf-original-price', $post->ID); // Here is the problem line.
$i++;
}
return $data;
}
add_action('rest_api_init', function() {
register_rest_route('wl/v1', 'bat-listing', [
'methods' => 'GET',
'callback' => 'wl_bat_listings',
]);
});
Hi,
Thank you for contacting us and I'd be happy to assist.
Reading the documentation, the "get_value" function needs to be called along with the proper class reference ( WP_REST_Meta_Fields::get_value ):
( https://developer.wordpress.org/reference/classes/wp_rest_meta_fields/get_value/ )
Perhaps you can try replacing the "get_value" function with the 'get_post_meta' function:
https://developer.wordpress.org/reference/functions/get_post_meta/
regards,
Waqar