Saving custom post types post_meta over REST-API fails

admin2025-06-05  1

I try to save with wp-json my custom post types meta data but does not works in my case

I create my custom post type with jjgrainger's class as it follows

$arguments = array(
        'public'       => true,
        'show_in_rest' => true,
    );

    $survey = new PostType('survey', $arguments);
    $survey->register();

    register_meta( 'survey', 'survey_data', array(
        'show_in_rest' => true,
        'single' => true
    ) );

    return $survey;

but when I save the following structure

 post: {
                    title: "Title",
                    content: "some content",
                    status: 'publish',
                    id: null,
                    survey_data: null,
                    meta: {
                        survey_data: [{'some': 'asdasd'}, {'some':'ssdfs'}]
                    }
                },

the post will be created but not the meta data

I try to save with wp-json my custom post types meta data but does not works in my case

I create my custom post type with jjgrainger's class as it follows

$arguments = array(
        'public'       => true,
        'show_in_rest' => true,
    );

    $survey = new PostType('survey', $arguments);
    $survey->register();

    register_meta( 'survey', 'survey_data', array(
        'show_in_rest' => true,
        'single' => true
    ) );

    return $survey;

but when I save the following structure

 post: {
                    title: "Title",
                    content: "some content",
                    status: 'publish',
                    id: null,
                    survey_data: null,
                    meta: {
                        survey_data: [{'some': 'asdasd'}, {'some':'ssdfs'}]
                    }
                },

the post will be created but not the meta data

Share Improve this question asked Dec 7, 2018 at 12:45 fefefefe 8943 gold badges14 silver badges34 bronze badges 1
  • please check url : stackoverflow/questions/35358088/… – vikrant zilpe Commented Dec 7, 2018 at 12:55
Add a comment  | 

1 Answer 1

Reset to default 0

The register_meta() function is defined like so:

function register_meta( $object_type, $meta_key, $args, $deprecated = null )

And the $object_type (as of now) has to be post for all post types. So use this:

register_meta( 'post', 'survey_data', ... )

See the // {comments} in the code below, which I copied from here:

<?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( ... );
register_meta( $object_type, 'my_meta_key', $args1 );

UPDATE

If you'd like to limit the meta to a certain post type, use the object_subtype parameter like so: (that parameter was added in WordPress version 4.9.8)

// Add survey_data meta support to `survey` post type in REST API.
register_meta( 'post', 'survey_data', array(
  'show_in_rest'   => true,
  'object_subtype' => 'survey',
  ...
) );

// Add survey_data meta support to `my_cpt` post type in REST API.
register_meta( 'post', 'survey_data', array(
  'show_in_rest'   => true,
  'object_subtype' => 'my_cpt',
  ...
) );

And as you can see, just duplicate the same register_meta() call for other post types. But the $object_type needs to be post, even for Pages (page post type).

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749119398a316527.html

最新回复(0)