Create taxonomy with meta term using the WP Rest Api

admin2025-06-06  5

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"
        ]
    }

});
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749194533a317154.html

最新回复(0)