• Boa Tarde Pessoal,

    Criei um Custom Post Type e dentro alguns metaboxes. Ao acessar a Rest API verifiquei que os valores dos metaboxes não constavam no arquivo JSON de retorno.
    Estou tentando inserir esses valores através da função abaixo:

    function my_get_post_meta() {
        // get_post_meta( post_id, meta_key[optional], single[optional] )
        $post_meta = get_post_meta( 22 );
        $meta = [];
        foreach( $post_meta as $meta_key => $meta_value ) {
            $meta[$meta_key] = $meta_value[0];
        }
        return $meta;
    }
    
    function meta_rest_api() {
        register_rest_field( 'imovel', 'desc_text', array(
            'get_callback'      => 'my_get_post_meta',
            // 'update_callback'=> null,
            // 'schema'         => null
        ) );
    }
    add_action( 'rest_api_init', 'meta_rest_api' );
    

    Quando executada ela adiciona os valores dos metaboxes a todos CPTs no arquivo JSON de retorno. Preciso que esses valores sejam adicionados ao seu respectivo post.

    Segue arquivo json com os dados aparecendo duplicados(campo desc_text)

    [
       {
          "id":31,
          "date":"2020-06-09T22:22:46",
          "date_gmt":"2020-06-10T01:22:46",
          "guid":{
             "rendered":"http:\/\/127.0.0.1:8712\/odin\/?post_type=imovel&p=31"
          },
          "modified":"2020-06-09T22:43:57",
          "modified_gmt":"2020-06-10T01:43:57",
          "slug":"apartamento-em-copacabana-3-quartos",
          "status":"publish",
          "type":"imovel",
          "link":"http:\/\/127.0.0.1:8712\/odin\/imovel\/apartamento-em-copacabana-3-quartos\/",
          "title":{
             "rendered":"Apartamento em copacabana 3 quartos"
          },
          "content":{
             "rendered":"\n<p>Now, what\u2019s happing here? Well, in the callback function I\u2019m getting all metadata from a post and adding them in an array as key-value pair. Then <code>register_rest_field<\/code> is doing the magic. WordPress provides this function to add data in the Rest API response object. It also knows in which route to put data. You also can provide <code>update_callback<\/code> and <code>schema<\/code> if necessary.<br>Now you will find all those metadata in <code>https:\/\/domain\/wp-json\/wp\/v2\/posts\/15<\/code> under <code>my_meta<\/code> property. Please note that I have provided the post id 15, in your case the post id will be different.<\/p>\n",
             "protected":false
          },
          "featured_media":0,
          "template":"",
          "localizacao":[
             2
          ],
          "tipo":[
             6
          ],
          "titulo_text":"",
          "desc_text":{
             "_edit_last":"1",
             "titulo_text":"Apt gavea",
             "desc_text":"lorem ipsum",
             "_edit_lock":"1591729195:1"
          },
          "rua_text":"Rua Nossa senhora de copabana",
          "comp_text":"apto 901",
          "cep_text":"23456-099",
          "gallery_imgs":"8,32,33",
          "_links":{
             "self":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/imovel\/31"
                }
             ],
             "collection":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/imovel"
                }
             ],
             "about":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/types\/imovel"
                }
             ],
             "wp:attachment":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/media?parent=31"
                }
             ],
             "wp:term":[
                {
                   "taxonomy":"localizacao",
                   "embeddable":true,
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/localizacao?post=31"
                },
                {
                   "taxonomy":"tipo",
                   "embeddable":true,
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/tipo?post=31"
                }
             ],
             "curies":[
                {
                   "name":"wp",
                   "href":"https:\/\/api.w.org\/{rel}",
                   "templated":true
                }
             ]
          }
       },
       {
          "id":22,
          "date":"2020-06-09T15:50:07",
          "date_gmt":"2020-06-09T18:50:07",
          "guid":{
             "rendered":"http:\/\/127.0.0.1:8712\/odin\/?post_type=imovel&p=22"
          },
          "modified":"2020-06-09T15:59:36",
          "modified_gmt":"2020-06-09T18:59:36",
          "slug":"rascunho-automatico",
          "status":"publish",
          "type":"imovel",
          "link":"http:\/\/127.0.0.1:8712\/odin\/imovel\/rascunho-automatico\/",
          "title":{
             "rendered":"Apartamento g\u00e1vea"
          },
          "content":{
             "rendered":"\n<p>Lorem ipsumiHIUQhwdiuqhwdiuhqwiudhiuqwdhuiqwduiqwhd<\/p>\n",
             "protected":false
          },
          "featured_media":0,
          "template":"",
          "localizacao":[
             5
          ],
          "tipo":[
    
          ],
        "titulo_text":"Apt gavea",
          "desc_text":{
             "_edit_last":"1",
             "titulo_text":"Apt gavea",
             "desc_text":"lorem ipsum",
             "_edit_lock":"1591729195:1"
          },
          "rua_text":"",
          "comp_text":"",
          "cep_text":"",
          "gallery_imgs":"",
          "_links":{
             "self":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/imovel\/22"
                }
             ],
             "collection":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/imovel"
                }
             ],
             "about":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/types\/imovel"
                }
             ],
             "wp:attachment":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/media?parent=22"
                }
             ],
             "wp:term":[
                {
                   "taxonomy":"localizacao",
                   "embeddable":true,
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/localizacao?post=22"
                },
                {
                   "taxonomy":"tipo",
                   "embeddable":true,
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/tipo?post=22"
                }
             ],
             "curies":[
                {
                   "name":"wp",
                   "href":"https:\/\/api.w.org\/{rel}",
                   "templated":true
                }
             ]
          }
       },
       {
          "id":7,
          "date":"2020-06-09T13:56:36",
          "date_gmt":"2020-06-09T16:56:36",
          "guid":{
             "rendered":"http:\/\/127.0.0.1:8712\/odin\/?post_type=imovel&p=7"
          },
          "modified":"2020-06-09T15:58:54",
          "modified_gmt":"2020-06-09T18:58:54",
          "slug":"casa",
          "status":"publish",
          "type":"imovel",
          "link":"http:\/\/127.0.0.1:8712\/odin\/imovel\/casa\/",
          "title":{
             "rendered":"Apto Flamengo"
          },
          "content":{
             "rendered":"\n<p>casa 2<\/p>\n",
             "protected":false
          },
          "featured_media":8,
          "template":"",
          "localizacao":[
             4
          ],
          "tipo":[
    
          ],
          "titulo_text":"Apartamento flamengo",
          "desc_text":{
             "_edit_last":"1",
             "titulo_text":"Apt gavea",
             "desc_text":"lorem ipsum",
             "_edit_lock":"1591729195:1"
          },
          "rua_text":"",
          "comp_text":"",
          "cep_text":"",
          "gallery_imgs":"",
          "_links":{
             "self":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/imovel\/7"
                }
             ],
             "collection":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/imovel"
                }
             ],
             "about":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/types\/imovel"
                }
             ],
             "wp:featuredmedia":[
                {
                   "embeddable":true,
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/media\/8"
                }
             ],
             "wp:attachment":[
                {
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/media?parent=7"
                }
             ],
             "wp:term":[
                {
                   "taxonomy":"localizacao",
                   "embeddable":true,
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/localizacao?post=7"
                },
                {
                   "taxonomy":"tipo",
                   "embeddable":true,
                   "href":"http:\/\/127.0.0.1:8712\/odin\/wp-json\/wp\/v2\/tipo?post=7"
                }
             ],
             "curies":[
                {
                   "name":"wp",
                   "href":"https:\/\/api.w.org\/{rel}",
                   "templated":true
                }
             ]
          }
       }
    ]
  • O tópico ‘WordPress API REST’ está fechado para novas respostas.