Trouble getting tribe_rest_single_event_data filter to do anything

Home Forums Calendar Products Events Calendar PRO Trouble getting tribe_rest_single_event_data filter to do anything

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1550126
    Canton
    Participant

    Hello,
    Following the documentation, I’m trying to add my custom fields to the output I get from the REST API, e.g. via

    http://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 ?

    #1550136
    Canton
    Participant

    I 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;
    }

    #1550592

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

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Trouble getting tribe_rest_single_event_data filter to do anything’ is closed to new replies.