Edit Post Types via API / Programmatically?

Support MB Custom Post Type Edit Post Types via API / Programmatically?

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #47937
    OliverOliver
    Participant

    Is it possible to edit the values of a Custom Post Type via API? Or some other programmatic way? (Not using the interface)

    For example I want to be able to change the 'Custom archive slug' and 'Custom rewrite slug' somehow.

    My use case is that I have a lot of websites I build from a template, and there's various things I'd want to switch up between builds (like the slugs of the post type as one example)

    #47944
    PeterPeter
    Moderator

    Hello,

    Yes, it is possible. WordPress supports a filter hook register_post_type_args so you can modify a post type setting. Please follow the documentation
    https://developer.wordpress.org/reference/hooks/register_post_type_args/

    #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

    #47951
    PeterPeter
    Moderator

    Hello,

    All the post type settings are stored in the post content, there is nothing to do with the custom field of the post type mb-post-type. Please check this screenshot https://imgur.com/ZZ7nHx7

    you can update the post type settings by updating the post content of the post type mb-post-type.

    If you want to get and update the post content of a post type, you have to include editor in the supports array for the content field to be included in the REST API response.

    Here is an example

    function enable_rest_api_for_metabox_post_type($args, $post_type) {
        if ($post_type === 'mb-post-type') {
            $args['show_in_rest'] = true;
            $args['supports'] = ['title', 'editor'];
        }
        return $args;
    }

    however, when you edit the post type in the admin area, the UI will be broken because editor is enabled by customization.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.