Support » Developing with WordPress » Cannot update taxonomy custom field via REST API (Wpapi)

  • I’ve created a custom taxonomy “vendors” with a custom field ‘vendor_email’ that I registered for REST API usage.

    With Wp-api I can correctly retrieve via API call to /wp-json/wp/v2/vendors/ all the data, also the custom field ‘vendor_email’. When I do a post or a put it works correctly on all the standard fields like name or description of the taxonomy “vendors”.

    But when I try to do a post or a put to create or update the custom fields ‘vendor_email’ it does not work. It seems that it actually cancel the value if already set

    I put all the details and the code here: https://stackoverflow.com/questions/70628195/cannot-update-taxonomy-custom-field-via-rest-api-wpapi

Viewing 4 replies - 1 through 4 (of 4 total)
  • Are you registering the meta as show in REST? Just checking as it wasn’t in your code detail anywhere

    e.g.

    $args1 = array( // Validate and sanitize the meta value.
            // Note: currently (4.7) one of 'string', 'boolean', 'integer',
            // 'number' must be used as 'type'. The default is 'string'.
            'type'         => 'string',
            // Shown in the schema for the meta key.
            'description'  => 'A meta key associated with a string meta value.',
            // Return a single value of the type.
            'single'       => true,
            // Show in the WP REST API response. Default: false.
            'show_in_rest' => true,
        );
     register_meta( 'vendors', 'vendor_email', $args1 );
    Thread Starter maubel

    (@maubel)

    Hi @alanfuller thanks for your reply. I’ve tried to add register_meta() on “rest_api_init” hook but I still have the same issue.

    This is my code now:

    // Register Vendor Email for REST API
    
    add_action( 'rest_api_init', 'register_rest_field_email' );
    
    function register_rest_field_email() {
        register_rest_field( 'vendors',
            'vendor_email',
            array(
                'get_callback'    => 'email_get_term_meta',
                'update_callback' => 'email_update_term_meta',
                'schema' => 'email_schema'
            )
        );
    }
    
    function email_get_term_meta($object) {
        return get_term_meta( $object['id'], 'vendor_email', true );
    }
    
    function email_update_term_meta( $value, $object, $field_name ) {
        // if ( ! $value || ! is_string( $value ) ) {
        //     return;
        // }
        return update_term_meta( $object->ID, $field_name, $value);
    }
    
    function email_schema($object) {
        return array(
            'vendor_email' => __( 'vendor_email' ),
            'type'        => 'string',
            'context' => array('view', 'edit')
        );
    }
    
    add_action( 'rest_api_init', 'register_posts_meta_field', 1 ); 
    
    function register_posts_meta_field() {
    
    $args1 = array( // Validate and sanitize the meta value.
        // Note: currently (4.7) one of 'string', 'boolean', 'integer',
        // 'number' must be used as 'type'. The default is 'string'.
        'type'         => 'string',
        // Shown in the schema for the meta key.
        'description'  => 'A meta key associated with a string meta value.',
        // Return a single value of the type.
        'single'       => true,
        // Show in the WP REST API response. Default: false.
        'show_in_rest' => true,
    );
    register_meta( 'vendors', 'vendor_email', $args1 );
    
    }
    

    My mistake, the register_meta object type for a CPT is ‘post’ not the CPT name

    add_action( 'rest_api_init', 'register_posts_meta_field', 1 ); 
    
    function register_posts_meta_field() {
    
    $args1 = array( // Validate and sanitize the meta value.
        // Note: currently (4.7) one of 'string', 'boolean', 'integer',
        // 'number' must be used as 'type'. The default is 'string'.
        'type'         => 'string',
        // Shown in the schema for the meta key.
        'description'  => 'A meta key associated with a string meta value.',
        // Return a single value of the type.
        'single'       => true,
        // Show in the WP REST API response. Default: false.
        'show_in_rest' => true,
    );
    register_meta( 'post', 'vendor_email', $args1 );
    
    }

    oops, and more – sorry should have checked
    edit and we need an object subtype

    add_action( 'rest_api_init', 'register_posts_meta_field', 1 ); 
    
    function register_posts_meta_field() {
    
    $args1 = array( 
        'object_subtype' => 'vendors',
        'type'         => 'string',
        'description'  => 'A meta key associated with a string meta value.',
        'single'       => true,
        'show_in_rest' => true,
    );
    register_meta( 'post', 'vendor_email', $args1 );
    
    }
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.