Support » Plugin: Events Manager » Rest API

  • Resolved colinaut

    (@colinaut)


    So I’m trying to get the events list to be accessed via REST. I know the plugin basically just makes a “event” custom post type so the following code should work to register it:

    add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
     
    function my_post_type_args( $args, $post_type ) {
     
        if ( 'event' === $post_type ) {
            $args['show_in_rest'] = true;
        }
     
        return $args;
    }

    When I do this call up the schema event shows up as a possible query so the “show_in_rest” is registering the event post_type. However when I make a GET call to it all I get is an empty array. Anyone know what I need to do to get a list of events?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi Colinaut,

    Just been looking at this myself today and added the following to functions.php

    add_filter( 'register_post_type_args', 'my_post_type_args', 10, 2 );
    
    function my_post_type_args( $args, $post_type ) {
    
        if ( 'event' === $post_type ) {
            $args['show_in_rest'] = true;
    
            // Optionally customize the rest_base or rest_controller_class
            $args['rest_base']             = 'events';
            $args['rest_controller_class'] = 'WP_REST_Posts_Controller';
        }
    
        return $args;
    }

    Now I can see my events listed within wp-json/wp/v2/events and query specific events. I’ve not managed to work out how to make custom meta appear yet.

    Guys,
    What are you trying to accomplish exactly? The event custom post type is not the actual EM_Event Object, so the default post query will probably not give the desired results.

    The code above allows the events to be listed via the wp-json api which in turn lets me use the data in an app I’m playing with. My only issue at the moment is getting the custom meta relating to these events exposed using the same method.

    My only issue at the moment is getting the custom meta relating to these events exposed using the same method.

    Again: what meta? Because I think, that is exactly what I meant. šŸ˜‰

    The custom post type only contains these meta keys by default. All other info is contained in the EM_Event Object.

    (
        [0] => _thumbnail_id
        [1] => _custom_booking_form
        [2] => _event_timezone
        [3] => _event_start_time
        [4] => _event_end_time
        [5] => _event_start
        [6] => _event_end
        [7] => _event_start_date
        [8] => _event_end_date
        [9] => _event_rsvp
        [10] => _event_rsvp_date
        [11] => _event_rsvp_time
        [12] => _event_rsvp_spaces
        [13] => _event_spaces
        [14] => _location_id
        [15] => _event_start_local
        [16] => _event_end_local
        [17] => _recurrence_id
        [18] => _event_id
    )
    

    The default information exposed via the REST api is what would be standard on a post, none of the fields listed above will display unless show_in_rest is set to true. Personally I’m only interested in exposing a few of these and custom attributes which area also in the postmeta table, I’ve tried adding variations of the following to functions.php

    // The object type. For custom post types, this is 'post';
    // for custom comment types, this is 'comment'. For user meta,
    // this is 'user'.
    $object_type = 'post';
    $args1 = array( // Validate and sanitize the meta value.
        // Note: currently (4.7) one of 'string', 'boolean', 'integer',
        // 'number' must be used as 'type'. The default is 'string'.
        'type'         => 'string',
        // Shown in the schema for the meta key.
        'description'  => 'A meta key associated with a string meta value.',
        // Return a single value of the type.
        'single'       => true,
        // Show in the WP REST API response. Default: false.
        'show_in_rest' => true
    );
    register_meta( $object_type, '_event_start_local', $args1 );

    I’m probably doing things completely wrong šŸ™‚

    I will stop following this topic now. Good luck.

    @colinaut I raised a query on the main wordpress support and from their testing the code above works, I’ve not had the same success.

    If you do get a chance to test could you let me know if you have any luck?

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Rest API’ is closed to new replies.