function __construct($post_type) { parent::__construct($post_type); // add the event_id metadata to newly created event instance posts add_action("rooftop_" . $this->post_type . "_rest_insert_post", function ($prepared_post, $request, $success) { update_post_meta($prepared_post->ID, 'price_list_id', $request['price_list_id']); update_post_meta($prepared_post->ID, 'ticket_type_id', $request['event_price_meta']['ticket_type_id']); update_post_meta($prepared_post->ID, 'price_band_id', $request['event_price_meta']['price_band_id']); return $prepared_post; }, 10, 3); add_action("rest_prepare_" . $this->post_type, function ($response, $post, $request) { $event_price_meta = get_post_meta($post->ID, 'event_price_meta', true); $response->data['event_price_meta'] = $event_price_meta; return $response; }, 10, 3); }
function __construct($post_type) { parent::__construct($post_type); // add the event_id metadata to newly created event instance posts add_action("rooftop_" . $this->post_type . "_rest_insert_post", function ($prepared_post, $request, $success) { update_post_meta($prepared_post->ID, 'event_id', $request['event_id']); $updated_meta = $request[$this->post_type . "_meta"]; // we need the event genre to be a top-level attribute (not a part of the event_meta array) // so that we can easily query related events that value if ($updated_meta && array_key_exists('genre', $updated_meta) && strlen($updated_meta['genre'])) { update_post_meta($request['id'], 'event_genre', $updated_meta['genre']); } else { delete_post_meta($request['id'], 'event_genre'); } return $prepared_post; }, 10, 3); }
function __construct($post_type) { parent::__construct($post_type); // add the event_id metadata to newly created event instance posts add_action("rooftop_" . $this->post_type . "_rest_insert_post", function ($prepared_post, $request, $success) { update_post_meta($prepared_post->ID, 'event_id', $request['event_id']); if ($request['price_list_id']) { update_post_meta($prepared_post->ID, 'price_list_id', $request['price_list_id']); } return $prepared_post; }, 10, 3); add_action("rest_prepare_" . $this->post_type, function ($response, $post, $request) { $event_instance_meta = get_post_meta($post->ID, 'event_instance_meta', true); // our clients depend on event_instance_meta being an object rather than an empty string - return an empty json node if get_post_meta returns "" $event_instance_meta = $event_instance_meta == "" ? json_decode("{}") : $event_instance_meta; $response->data['event_instance_meta'] = $event_instance_meta; $response->data['price_list_id'] = get_post_meta($post->ID, 'price_list_id', true); return $response; }, 10, 3); }