Support

Account

Home Forums General Issues Can’t send a string via REST API into a select field

Solved

Can’t send a string via REST API into a select field

  • So, this is my problem. I’ve tried sending a string type into my meta “observacao_intensidade_ataque” but it gives me this error:

    {
    "code": "rest_invalid_param",
    "message": "Parâmetros inválidos: acf",
    "data": {
    "status": 400,
    "params": {
    "acf": "acf[observacao_intensidade_ataque] não é um de "
    },
    "details": {
    "acf": {
    "code": "rest_invalid_param",
    "message": "acf[observacao_intensidade_ataque] não é um de ",
    "data": {
    "param": "acf[observacao_intensidade_ataque]",
    "value": "testing"
    }
    }
    }
    }
    }

    If I send it as array it saves though:

    "observacao_intensidade_ataque": {
    "value": "1",
    "label": "Teste"
    },

    Problem is, as an array, it does not work correctly although it get’s saved into my database.

    I have other fields, all working as expected.

    I’ve even checked here and it seems the select type field is supposed to support a “string” type – which obviously makes sense because that’s how it’s saved as a meta anyway.

    I’ve tried to find people complaining about same problem but, strangely.

    I’m using ACF Pro 6.2.9.

  • Due to the lack of answers here, I had to find a way to make this happen so I ended up simply adding it to the standard REST like so:

    function save_custom_meta_from_rest( $post, $request, $creating ) {
        $params = $request->get_json_params();
        
        if ( isset( $params['observacao_intensidade_ataque'] ) ) {
            update_post_meta( $post->ID, 'observacao_intensidade_ataque', sanitize_text_field( $params['observacao_intensidade_ataque'] ) );
        }
    
        if ( isset( $params['observacao_incidencia_doenca_parcela'] ) ) {
            update_post_meta( $post->ID, 'observacao_incidencia_doenca_parcela', sanitize_text_field( $params['observacao_incidencia_doenca_parcela'] ) );
        }
    }
    add_action( 'rest_insert_observacao', 'save_custom_meta_from_rest', 10, 3 );

    Hope this helps future people with the same problem.

    I just have now to put these two fields outside “acf” and that’s it.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.