$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'); ?>Create taxonomy with meta term using the WP Rest Api|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)

Create taxonomy with meta term using the WP Rest Api

matteradmin8PV0评论

I am trying to create taxonomy elements (the taxonomies are already registered) from the front end using the REST Api v2. I am able to do so except not able to save the meta fields from the taxonomies.


I have a registered taxonomy ("place") and I am trying to create elements for it using the Rest Api.

The taxonomy has a term meta ("my_meta"). I am able to get the information from the taxonomy:

add_action( 'rest_api_init', 'slug_register_meta' );
function slug_register_meta() {
    register_rest_field( 'place',
        'meta', 
        array(
            'get_callback'    => 'slug_get_meta',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}
function slug_get_meta( $object, $field_name, $request ) {
    return get_term_meta( $object[ 'id' ] );
}

which lets me get the information when I access: /wp-json/wp/v2/place/53

{
    "id": 53,
    "count": 0,
    ...
    "taxonomy": "place",
    "meta": {
        "my_meta": [
            "the meta value"
        ]
    },
    ...
}

I can register a new taxonomy element through JavaScript:

var place_new = new wp.api.models.Place({
    name: 'the name',// works
    description: 'the description',// works

    my_meta: 'test1',// doesn't work

    fields: {// doesn't work
        my_meta: 'test3'
    },

    meta: {// doesn't work
        my_meta: 'test2'
    }

    });

place_new.save();

The problem is the my_meta value won't save, I am not sure how to refer to it or if there is some PHP I am missing.

I am trying to create taxonomy elements (the taxonomies are already registered) from the front end using the REST Api v2. I am able to do so except not able to save the meta fields from the taxonomies.


I have a registered taxonomy ("place") and I am trying to create elements for it using the Rest Api.

The taxonomy has a term meta ("my_meta"). I am able to get the information from the taxonomy:

add_action( 'rest_api_init', 'slug_register_meta' );
function slug_register_meta() {
    register_rest_field( 'place',
        'meta', 
        array(
            'get_callback'    => 'slug_get_meta',
            'update_callback' => null,
            'schema'          => null,
        )
    );
}
function slug_get_meta( $object, $field_name, $request ) {
    return get_term_meta( $object[ 'id' ] );
}

which lets me get the information when I access: /wp-json/wp/v2/place/53

{
    "id": 53,
    "count": 0,
    ...
    "taxonomy": "place",
    "meta": {
        "my_meta": [
            "the meta value"
        ]
    },
    ...
}

I can register a new taxonomy element through JavaScript:

var place_new = new wp.api.models.Place({
    name: 'the name',// works
    description: 'the description',// works

    my_meta: 'test1',// doesn't work

    fields: {// doesn't work
        my_meta: 'test3'
    },

    meta: {// doesn't work
        my_meta: 'test2'
    }

    });

place_new.save();

The problem is the my_meta value won't save, I am not sure how to refer to it or if there is some PHP I am missing.

Share Improve this question edited Dec 29, 2016 at 11:43 Alvaro asked Dec 28, 2016 at 18:06 AlvaroAlvaro 2,5932 gold badges28 silver badges29 bronze badges 4
  • Did you solve this? – input Commented Apr 7, 2017 at 8:57
  • @input No, it was for a side project. If I come back to it I would probably try MB Term Meta, MB REST API if I dont find the way. – Alvaro Commented Apr 7, 2017 at 9:23
  • Posted an answer here stackoverflow/a/50397449/1654250 if you're interested – Craig Wayne Commented May 18, 2018 at 11:21
  • This worked for my case stackoverflow/a/62490342/441016. – 1.21 gigawatts Commented Jun 20, 2020 at 19:03
Add a comment  | 

2 Answers 2

Reset to default 4

I think you need an update_callback in register_rest_field(). Please note that I haven't tested this.

add_action( 'rest_api_init', 'slug_register_meta' );
function slug_register_meta() {
    register_rest_field( 'place',
        'meta', 
        array(
            'get_callback'    => 'slug_get_meta',
            'update_callback' => 'slug_update_meta',
            'schema'          => null,
        )
    );
}
function slug_get_meta( $object, $field_name, $request ) {
    return get_term_meta( $object[ 'id' ] );
}
function slug_update_meta($value, $object, $field_name){
    // please note: make sure that $object is indeed and object or array
    return update_post_meta($object['id'], $field_name, $value);
}

You should pass your meta value as an array to your meta key.

var place_new = new wp.api.models.Place({
    name: 'the name',// works
    description: 'the description',// works

    meta: {
        "my_meta": [
            "new_meta_value"
        ]
    }

});
Post a comment

comment list (0)

  1. No comments so far