0

I have register a post meta field called project_description with it's explicit 'schema' in register_post_meta function. The following is register_post_meta function -

function register_project_description_post_meta() {
    register_block_type( plugin_dir_path( __FILE__) . 'build/project-description' );

    register_post_meta('project', 'project_description', array(
        'single' => true,
        'type' => 'object',
        'show_in_rest' => array(
            'schema' => array(
                'type' =>'object',
                'properties' => array(
                    'excerpt' => array(
                        'type' => 'object',
                        'properties' => array(
                            'title' => array(
                                'type' => 'string'
                            ),
                            'description' => array(
                                'type' => 'string'
                            )
                        )
                    ),
                    'responsive' => array(
                        'type' => 'object',
                        'properties' => array(
                            'title' => array(
                                'type' => 'string'
                            ),
                            'description' => array(
                                'type' => 'string'
                            ),
                            'image' => array(
                                'type' => 'string',
                                'properties' => array(
                                    'id' => array(
                                        'type' => 'integer'
                                    ),
                                    'url' => array(
                                        'type' => 'string'
                                    )
                                )
                            )
                        )
                    )
                                )
                        )
        )
    ));
}

Now when I change the image property with type string instead of object I get project_description: null when calling REST API in Postman.

So I want to know is there a way to change the schema when a post already has that meta field. That is, after the schema change the meta field should look like this -

project_description: {
   excerpt: {
      title: '',
      description: '',
   },
   responsive: {
      title: '',
      description: '',
      image: ''
   }
}

instead of

project_description: {
   excerpt: {
      title: '',
      description: '',
   },
   responsive: {
      title: '',
      description: '',
      image: {
        id: '',
        url: ''
      }
   }
}
  • 1
    Such a change would invalidate the format of data already stored under the old schema. How would WordPress know how to read it? It looks like you would first have to perform an extract, transform and re-save operation directly on the database before updating the theme/child theme/plugin with the new schema. The transformation process could use functions like [maybe_unserialize](https://developer.wordpress.org/reference/functions/maybe_unserialize/), and direct calls to [wpdb::update()](https://developer.wordpress.org/reference/classes/wpdb/update/) to save converted data. – kofeigen Apr 28 '23 at 02:33

0 Answers0