• raphaelnikson

    (@raphaelnikson)


    I’m trying to update a custom field, but when I run the route through POSTMAN it does not update the field. Creates a new record. The route is something like:
    $POST …wp/v2/posts/362/meta?key=servicos_status&value=pause

    Someone help me?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Pavel

    (@pavelk27)

    Hi,

    Are you sure that your callback function gets called? Can you try to put debug code there, just to see if it is. I’ve had similar issues when I declared callback function incorrectly, e.g.

    $defaults = array(
            'get_callback'    => test_callback(),
        );

    instead of:

    $defaults = array(
            'get_callback'    => array( $this, 'test_callback' ),
        );

    when used in class scope.

    Thanks

    Thread Starter raphaelnikson

    (@raphaelnikson)

    Thanks man.. Yes.. look

    add_action( 'rest_api_init', function () {
        register_rest_field( array('servicos', 'posts'),
            'servico_status',
            array(
                'get_callback'    => 'get_field_servico_status',
                'update_callback' => 'slug_update_servicos',
                'schema' => array(
                    'description' => 'My special field',
                    'type' => 'string',
                    'context' => array('view', 'edit')
                )
            )
        );
    });
    function get_field_servico_status( $post, $field_name, $request ) {
        return get_post_meta( $post[ 'id' ], $field_name, true );
    }
    function slug_update_servicos($value, $post, $field_name) {
        if (!$value || !is_string($value)) {
            return;
        }
        return update_post_meta($post->ID, $field_name, strip_tags($value));
    }
    Pavel

    (@pavelk27)

    Are you sure that slug_update_servicos() gets the arguments you’re checking and using in it? Can you try to debug all of the arguments by printing their values into error_log, i.e.:

    error_log( print_r( $value, 1 ) );
    error_log( print_r( $post, 1 ) );
    error_log( print_r( $field_name, 1 ) );
    

    Related question:
    when a content is created, a HTTP 201 is returned and returned JSON contains the new data *including the custom field*.
    I don’t understand how the update_callback could define the value given to the returned custom field.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘register_rest_field update_callback not work’ is closed to new replies.