Home › Forums › Calendar Products › Events Calendar PRO › Custom Field output for JSON feed
- This topic has 10 replies, 3 voices, and was last updated 8 years ago by
Marianne Steiner.
-
AuthorPosts
-
April 4, 2018 at 12:36 pm #1496819
Marianne Steiner
ParticipantI tried before on this thread and try to reword my question since I didn’t get a satisfying answer and still couldn’t figure it out myself:
We need to outputs some custom fields on the JSON list of events to consume them on another website.
I figured out how to add the custom fields to a single event output, like e.g http://elternbildung-aargau.ch/wp-json/tribe/events/v1/events/2248 (it’s not in here, since this is the live site and I only added it on my local installation, but to show you what I mean)
I did this with the code mentioned here in the article:
https://theeventscalendar.com/knowledgebase/introduction-events-calendar-rest-api/and also explained in my previous ticket:
What I need to accomplish is have the output on the “archive”/list of events, e.g an URL like:
http://elternbildung-aargau.ch/wp-json/tribe/events/v1/eventsThis I couldn’t figure out and would love to get some support.
Yes, this is not basic user support, but it’s developer support, which should be already documented.
I just can’t find this in the documentation and searching fortribe_rest_events_archive_datadoesn’t give any results.Should I better open an issue on GitHub for this?
But it’s not a real issue, just a question.April 6, 2018 at 12:43 pm #1499104Victor
MemberHi Marianne!
I’m sorry for the delay in getting back to you. We’ve been dealing with a higher number of threads than usual, which caused us some delays.
First, please let me note that we are fairly limited in how much support we can give for custom development questions like that. But, we always like helping out and at least point users into the right direction as much possible.
You seem to be on the right track with the ‘tribe_rest_events_archive_data’ filter. As a reference, you will find it in >Β https://github.com/moderntribe/the-events-calendar/blob/master/src/Tribe/REST/V1/Endpoints/Archive_Event.php#L176
There is an available endpoint for the REST API documentation atΒ /wp-json/tribe/events/v1/doc You can use the Swagger editor to visualize it better >Β https://editor.swagger.io/
If you bump into an issue while using the filters available, please don’t hesitate to share the code and we’ll try to help as much as we can.
Best,
VictorApril 6, 2018 at 11:07 pm #1499412momofone
ParticipantYou may find this a bit more useful if you’ve got version 4.5 or later
add_filter( 'tribe_rest_event_data', 'sp_add_custom_fields' ); function sp_add_custom_fields( $data, $event ) { $event_id = $data['id']; $custom_fields = get_post_custom($event_id); foreach ( $custom_fields as $key => $value ) { $data = array_merge( $data, array( $key => $value, )); return $data; } }Since it just scoops up the custom fields and adds them to the end of the response – after the organizer. It adds to both the single and the archive.
April 9, 2018 at 7:02 am #1500507Victor
MemberHi Jacqui!
Thanks for chiming in here to share that snippet! π
Marianne, you could try that snippet as an example and see if it works on your end.
Let us know if any follow up questions.
Best,
VictorApril 10, 2018 at 3:06 pm #1503593Marianne Steiner
ParticipantThanks very much.
I have version 4.6.13 of The Events Calendar and version of 4.4.24.2 of The Events Calendar PRO
Sadly the code gives an error:
Fatal error: Uncaught ArgumentCountError: Too few arguments to function sp_add_custom_fields(), 1 passed in /app/public/wp-includes/class-wp-hook.php on line 288 and exactly 2 expected in /app/public/wp-content/themes/Avada-Child-Theme/functions.php:42 Stack trace: #0 /app/public/wp-includes/class-wp-hook.php(288): sp_add_custom_fields(Array) #1 /app/public/wp-includes/plugin.php(203): WP_Hook->apply_filters(Array, Array) #2 /app/public/wp-content/plugins/the-events-calendar/src/Tribe/REST/V1/Post_Repository.php(148): apply_filters('tribe_rest_even...', Array, Object(WP_Post)) #3 /app/public/wp-content/plugins/the-events-calendar/src/Tribe/REST/V1/Endpoints/Archive_Event.php(164): Tribe__Events__REST__V1__Post_Repository->get_event_data(5433) #4 /app/public/wp-includes/rest-api/class-wp-rest-server.php(936): Tribe__Events__REST__V1__Endpoints__Archive_Event->get(Object(WP_REST_Request)) #5 /app/public/wp-includes/rest-api/class-wp-rest-server.php(321): WP_REST_Server->dispatch(Object(WP_REST_Request)) #6 /app/public/wp-in in /app/public/wp-content/themes/Avada-Child-Theme/functions.php on line 42I guess it’s a silly typo kind of an error, but just can’t see it now.
I will have a look at it during day time (it’s after midnight now) but if you see it right away, I’d be happy with any hints π
Thanks again.
We’re getting closer, that’s for sure!
April 10, 2018 at 5:48 pm #1503687momofone
ParticipantYou could try grabbing the fields by key name e.g. _ecp_custom_1, _ecp_custom_2 etc.
Using:
add_filter( 'tribe_rest_event_data', 'sp_add_event_price' ); function sp_add_event_price($data, $event) { $event_id = $data['id']; $data = array_merge( $data, array( 'price' => get_post_meta( $event_id, '_ecp_custom_2', true ), 'price2' => get_post_meta( $event_id, '_ecp_custom_3', true ), )); return $data; }April 10, 2018 at 10:00 pm #1503832momofone
ParticipantI’m not sure what version of PHP you have but you could try this also:
function sp_add_event_price($data) {April 11, 2018 at 7:52 am #1504330Victor
MemberHi Jacqui and Marianne!
You might want to try adding the priority and number of parameters being passed to the filter:
add_filter( 'tribe_rest_event_data', 'sp_add_custom_fields', 10, 2 );Hope that helps. π
Best,
VictorApril 11, 2018 at 1:16 pm #1504735Marianne Steiner
ParticipantThanks very much Victor and Jacqui
I was able to figure it out with your hints and help.
It works as expected like this, composing your answers:
// this filter adds the custom field values to the json output add_filter( 'tribe_rest_event_data', 'sp_add_event_customfields' ); function sp_add_event_customfields($data) { $event_id = $data['id']; $data = array_merge( $data, array("custom_fields" => array( 'price' => get_post_meta( $event_id, '_ecp_custom_2', true ), 'Referent' => get_post_meta( $event_id, '_ecp_custom_3', true ), 'Details_Anmeldung' => get_post_meta( $event_id, '_ecp_custom_4', true ), 'Fotorechte' => get_post_meta( $event_id, '_ecp_custom_7', true ), 'Preis' => get_post_meta( $event_id, '_ecp_custom_16', true ), ) )); return $data; }April 11, 2018 at 2:00 pm #1504813Marianne Steiner
ParticipantNow I have one problem left:
How can I output the “Recurrence Description field in JSON feed“?I opened a separate topics for this so that I can close this thread since I couldn’t find any hint in the documentation.
-
This reply was modified 8 years ago by
Marianne Steiner.
April 12, 2018 at 4:30 am #1505275Victor
MemberRight on Marianne!
Glad to know you could work it out! Thanks for following up with this and sharing the code so other users can make use of it. π
Iβll go ahead and close this now, we’ll try to help with your other request in the linked thread.
Cheers,
Victor -
This reply was modified 8 years ago by
-
AuthorPosts
- The topic ‘Custom Field output for JSON feed’ is closed to new replies.
