Home › Forums › Calendar Products › Events Calendar PRO › Trouble getting tribe_rest_single_event_data filter to do anything
- This topic has 2 replies, 2 voices, and was last updated 5 years, 11 months ago by Canton.
-
AuthorPosts
-
June 9, 2018 at 8:33 pm #1550126CantonParticipant
Hello,
Following the documentation, I’m trying to add my custom fields to the output I get from the REST API, e.g. viahttp://127.0.0.1:8060/wp-json/tribe/events/v1/events
I don’t think my filter is firing off.
As a test, I added this to my functions.php file:
add_filter( ‘tribe_rest_single_event_data’, ‘raccoon_add_suspect_level’ );
function raccoon_add_suspect_level( array $event_data ) {
exit;
}You would think this would break my REST API results, but they’re still coming back fine.
Am I not invoking this filter correctly? Or does this filter not impact the output from /wp-json/tribe/events/v1/events ?June 9, 2018 at 8:58 pm #1550136CantonParticipantI solved this. The REST documentation only mentions tribe_rest_single_event_data, which is only good for modifying the output from getting the output from a single event.
I dug through the source code and discovered the tribe_rest_event_data filter, which is what modifies the output of the more general /wp-json/tribe/events/v1/events endpoint.
May I recommend that the documentation be updated to include a list of all the available filters? (Or at least more than just the single filter?)
For anyone who’s looking for a way to add the custom fields to the multi-event REST output, here’s sample code for your functions.php file. Note the commented out line that lets you discover what mysterious _ecp_custom_x meta name was assigned for each of your custom fields.
add_filter( ‘tribe_rest_event_data’, ‘api_add_custom_values_all’ );
function api_add_custom_values_all( array $event_data ) {
// $meta = get_post_meta($event_id); print_r($meta); exit; // use this to figure out custom field names e.g. _ecp_custom_2
$event_id = $event_data[‘id’];
$max_participants = get_post_meta( $event_id, ‘_ecp_custom_3’, true );
$event_data[‘max_participants’] = $max_participants;
$attendance = get_post_meta( $event_id, ‘_ecp_custom_2’, true );
$event_data[‘attendance’] = $attendance;
return $event_data;
}June 11, 2018 at 5:15 am #1550592Jaime MarchwinskiKeymasterGlad you were able to resolve your issue and thanks for sharing! You are welcome back in our support forums any time!
Since you marked this thread as Resolved, I am going to close this thread.
Have a great week!
-
AuthorPosts
- The topic ‘Trouble getting tribe_rest_single_event_data filter to do anything’ is closed to new replies.