Wordpress Rest API with advanced custom fields Goo

2019-08-16 01:11发布

问题:

I want to use a Wordpress backend with advanced custom fields and the json Wordpress API to serve data to a mobile app.

For this project the client needs to select a location in the maps custom field. The API should return a json array with some other data and the data from the Google maps field.

The object the API returns is formatted as follows:

{
"0": [
    {
      "title": "Test 1",
      "evenement_afbeelding": "38",
      "kunstenaars": "Name One",
      "openingstijden": "9:00 - 18:30",
      "evenementbeschrijving": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ac pharetra tortor.",
      "evenement_lokatie": "a:3:{s:7:\"address\";s:40:\"Jansweg 50, 2011 KN Haarlem, Netherlands\";s:3:\"lat\";s:17:\"52.38513460768028\";s:3:\"lng\";s:17:\"4.638633728027344\";}",
      "biografie": "<strong>Lorem ipsum dolor sit amet</strong>\r\n\r\nConsectetur adipiscing elit. Nullam ac pharetra tortor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."
    }
],
    "status": "ok"
}

The weird thing is that the value on key "evenement_lokatie" (the Google maps field) returns a string that I cannot convert to an array. The value on the "evenement_lokatie" key should contain another array like this:

{
"0": [
    {
      "title": "Test 1",
      "evenement_afbeelding": "38",
      "kunstenaars": "Name One",
      "openingstijden": "9:00 - 18:30",
      "evenementbeschrijving": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam ac pharetra tortor.",
      "evenement_lokatie": [
        {
          "Stad": "Haarlem",
          "lang": "32143241",
          "lat": "721321"
        }
  ],
      "biografie": "<strong>Lorem ipsum dolor sit amet</strong>\r\n\r\nConsectetur adipiscing elit. Nullam ac pharetra tortor. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."
    }
],
    "status": "ok"
}

This is the PHP code that should format the data in wordpress:

<?php


class json_api_events_controller{

    public function hello_world(){
        return array('message'=>'Hello World!');
    }

    public function get_events(){
        $array = array();


        $args = array('post_type'=>'evenementen', 'posts_per_page'=>-1, 'order_by'=>'title', 'order'=>'ASC');
        $loop = new WP_Query($args);

        $counter = 0;

        while ( $loop->have_posts() ) : $loop->the_post();

            $id = get_the_id();
            $custom = get_post_custom($id);

            // add the title
            $array[$id][$counter]['title'] = get_the_title();

            // add all the custom fields
            foreach($custom as $k => $v){

                // if key starts with '_' symbol, don't add to the array
                if(strpos($k, '_') !== 0) {

                    $array[$id][$counter][$k] = array_shift($v);

                }
            }

            $counter++;
        endwhile;


        return $array;
    }

}

Also there are weird chracters in the string, like ";s:17:" and ";s:40:". Can anyone show me how I can convert such a string to an array like I described?

Thanks in advance!

回答1:

The "weird" value is actually serialized PHP - you can deserialize it serverside using unserialize before you json_encode, and you should be fine.

Wordpress serializes metadata in this way, so that any object can be saved as strings. The trick is to use PHP deserialization to get a valid PHP object before creating the valid JSON object.

Hope this helps.



回答2:

The way I resolved it is to query the posts, get the location field and it's data, add two meta fields for lat and lng.

The query:

  while( $query->have_posts() ) : $query->the_post();
    $id = get_the_ID();
    $location = get_field('location');
    $lat = $location['lat'];
    $lng = $location['lng'];
    if (! metadata_exists( 'post', $id, 'lat' ) ) {
      add_post_meta($id, 'lat', $lat, true);
    }
    if (! metadata_exists( 'post', $id, 'lng' ) ) {
      add_post_meta($id, 'lng', $lng, true);
    }

And then since I am using javascript, I am reading that data like this:

      $.each(data, function(i) {
        var lat = data[i]["details"]["lat"];
        var lng = data[i]["details"]["lng"];