Custom Field output for JSON feed

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.
Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #1496819
    Marianne Steiner
    Participant

    I 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:

    custom fields and series data for list

    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/events

    This 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 for tribe_rest_events_archive_data doesn’t give any results.

    Should I better open an issue on GitHub for this?
    But it’s not a real issue, just a question.

    #1499104
    Victor
    Member

    Hi 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,
    Victor

    #1499412
    momofone
    Participant

    You 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.

    #1500507
    Victor
    Member

    Hi 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,
    Victor

    #1503593
    Marianne Steiner
    Participant

    Thanks 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 42
    

    I 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!

    #1503687
    momofone
    Participant

    You 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;
    }
    #1503832
    momofone
    Participant

    I’m not sure what version of PHP you have but you could try this also:

    function sp_add_event_price($data) {

    #1504330
    Victor
    Member

    Hi 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,
    Victor

    #1504735
    Marianne Steiner
    Participant

    Thanks 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;
    }
    
    #1504813
    Marianne Steiner
    Participant

    Now 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.

    #1505275
    Victor
    Member

    Right 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

Viewing 11 posts - 1 through 11 (of 11 total)
  • The topic ‘Custom Field output for JSON feed’ is closed to new replies.