• Resolved snorklebum

    (@snorklebum)


    Hi,

    I am trying to expose the events manager plugin data via wp-json.

    So far I have added the following code block 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;
    }

    This exposes the events list via wp-json/wp/v2/events/ and individual events with wp-json/wp/v2/events/<id>

    The events themselves have specific data held in the postsmeta table that I’m also looking to expose, ie.
    meta_key _event_start_local, _event_rsvp, etc.

    add_action( 'init', 'register_new_meta');
    
    function register_new_meta() {
         $args = array(
            'type' => 'string',
            'description' => 'Event start date time',
            'single' => true,
            'show_in_rest' => true,
        );
        register_meta( 'post', '_event_start_local', $args );
    }

    The above changes the post json but changing this to event does not seem to change the events json, any advice on what I’m missing would be great.

    Thanks

Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator bcworkz

    (@bcworkz)

    You are conflating post types and object types. All post types (event, page, post, etc.) are all still of the same “post” object type. The first argument for post_meta() is for the related object type, not post type. Object types relate to which table the data is stored in. The only object types in WP are users, taxonomies, comments, or posts.

    Thread Starter snorklebum

    (@snorklebum)

    Thanks for the reply, apologies if I’ve misunderstood but you seem to be implying that the code above is correct(register_meta( ‘post’,..), the events data I am trying to expose is in the wp_postmeta table.

    Moderator bcworkz

    (@bcworkz)

    “post” IS the correct argument because you are passing an object type. Your event post type is still a post object type. When you request your events route when “post” is passed to register_meta(), the wp_postmeta data for events should be included in the return.

    If your code is working as you want with “post” passed, the only trouble seems to be you wanting to pass “event” instead. That would be incorrect as there is no “event” object type in WP.

    Thread Starter snorklebum

    (@snorklebum)

    So I’m using post and when I navigate to wp-json/wp/v2/pages ot wp-json/wp/v2/posts I see the meta fields and obviously they are currently “” as I’d expect. When I go to wp-json/wp/v2/events the normal entries are show but no meta entry. Is there anyway to extend this so that custom types meta is supported?

    Moderator bcworkz

    (@bcworkz)

    I tested your code on my site, passing “post” to register_meta() of course. When I requested wp-json/wp/v2/events/1242 (the ID of my test event post), the meta data is included in the response. IOW, your code works fine. If you are not getting post meta back on your site, I have to think it’s related to a theme or plugin conflict elsewhere and not due to your code itself.

    Thread Starter snorklebum

    (@snorklebum)

    Well this is frustrating, at the moment I’m testing this on a clean build site with twenty-nineteen and events manager is the only plugin.

    On posts I see this with the two additional fields

    On events I see the standard wp meta but not those asked for in register_meta

    I’ll see if any of the others who have been trying to do the same have had any further luck.

    Thanks for looking in to this with me.

    Moderator bcworkz

    (@bcworkz)

    It must be something about how the events post type is registered then. I created the post type myself using arguments I typically use. I do not have events manager active. It’s the only difference between our “clean builds”.

    Timothy Jacobs

    (@timothyblynjacobs)

    Make sure your post type has custom-fields in supports @snorklebum.

    You can also use the register_post_meta() introduced in WP 4.9.8 to only register the meta key for your post type.

    register_post_meta( 'event', 'meta_key', $args );

    Thread Starter snorklebum

    (@snorklebum)

    @timothyblynjacobs Thank you so much, I used post_type_supports() to check for ‘custom-fields’ and it was indeed disabled, I believe this is to stop them being editable via edit. I’ve just hidden the meta boxes to get around this. Now my fields are appearing in the JSON results and I can get on with creating my application ??

    Thanks @timothyblynjacobs! My custom post type also didn’t have support for custom-fields. The post meta is now being saved correctly. Glad I found this discussion ??

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Adding postmeta to wp-json custom post type’ is closed to new replies.