Reputation: 119
I have this function to for my custom fields to save in WP data base (this is working and saving):
function notifyem_save_post() {
if(empty($_POST)) return; //why is prefix_teammembers_save_post triggered by add new?
global $post;
update_post_meta($post->ID, "country", $_POST["country"]);
update_post_meta($post->ID, "region", $_POST["region"]);
update_post_meta($post->ID, "time", $_POST["time"]);
update_post_meta($post->ID, "activity", $_POST["activity"]);
}
I want to display them from REST API but only TITLE is showing:
[{"title":"john"},{"title":"xxx"},{"title":"11"}]
This is my code for my REST API GET
function notifyem_rest_get() {
$subscriptions = new WP_Query(array(
'post_type' => 'notifyem'
));
$subscriptionResults = array();
register_rest_route('notifyemAPI/v1', '/subscription', array(
'methods' => WP_REST_SERVER::READABLE,
'callback' => array($this, 'getSubscription')
));
}
function getSubscription() {
$subscriptions = new WP_Query(array(
'post_type' => 'notifyem',
'meta_key' => 'country'
));
$subscriptionResults = array();
while($subscriptions->have_posts()) {
$subscriptions->the_post();
array_push($subscriptionResults, array(
'region' => get_field('regi'),
'country' => get_field('country'),
'activity' => get_field('activity'),
'time' => get_field('time')
));
}
return $subscriptionResults;
}
The screenshow below are the fields inserted to my custom post type.
Any ideas how to get the custom fields I created in my REST API?
Upvotes: 1
Views: 446
Reputation: 9342
Try this code
function getSubscription() {
$subscriptions = new WP_Query(array(
'post_type' => 'notifyem',
'meta_key' => 'country'
));
$subscriptionResults = array();
while($subscriptions->have_posts()) {
$subscriptions->the_post();
array_push($subscriptionResults, array(
'region' => get_post_meta(the_ID(), "region", true),
'country' =>get_post_meta(the_ID(), "country", true),
'activity' => get_post_meta(the_ID(), "activity", true),
'time' => get_post_meta(the_ID(), "time", true)
));
}
return $subscriptionResults;
}
Upvotes: 0