Support Forum » User Profile

Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • in reply to: ℹ️Edit Post Types via API / Programmatically? #47946
    OliverOliver
    Participant

    Thanks Peter.

    So I've created a code snippet and managed to pull with API 'mb-post-type'. But I don't get back the fields from 'mb-post-type' (when make the request to the post type and Id, like: /wp/v2/mb-post-type/112). The only thing I get are 'slug', 'status', 'type' etc. How can I get back the fields?

    For example I want to be able to see and update the values for these under 'Advanced': https://loom.com/i/73eb07b1ec7c44b293f16d9a73715b2e

    This is my code snippet:

    // Enable REST API for mb-post-type only
    function enable_rest_api_for_metabox_post_type($args, $post_type) {
        if ($post_type === 'mb-post-type') {
            $args['show_in_rest'] = true;
        }
        return $args;
    }
    add_filter('register_post_type_args', 'enable_rest_api_for_metabox_post_type', 10, 2);
    
    // Add all meta keys to REST API for mb-post-type
    function add_all_meta_to_rest_api() {
        register_rest_field(
            'mb-post-type',
            'all_meta',
            array(
                'get_callback' => function($post) {
                    return get_post_meta($post['id']);
                }
            )
        );
    }
    add_action('rest_api_init', 'add_all_meta_to_rest_api');

    I've done a lot of back and forth with Claude to try and expose everything - but I can't figure out how to see the fields

Viewing 1 post (of 1 total)