$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Saving custom post types post_meta over REST-API fails|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Saving custom post types post_meta over REST-API fails

matteradmin9PV0评论

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).

Post a comment

comment list (0)

  1. No comments so far