Skip Navigation

[Resolved] Using custom fields in the wordpress api, what function do I use?

This support ticket is created 2 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 2 years, 2 months ago.

Assisted by: Waqar.

Author
Posts
#2502403

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',
]);
});

#2502737

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