首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过wordpress rest发布未序列化的数组作为元数据

如何通过wordpress rest发布未序列化的数组作为元数据
EN

WordPress Development用户
提问于 2021-07-23 10:00:45
回答 1查看 820关注 0票数 1

我刚接触过wordpress,目前正在使用WordPress Rest帮助使用现有数据自动创建自定义文章。所需的字段之一是图像is数组,以便能够显示图像。如果文章是在wordpress上用图像手动创建的,我知道元数据需要采用这种格式

代码语言:javascript
复制
"fave_property_images":["18339","18337","18321","17794","17793","17792","17791","17790","17789","17787"]

但是,在通过WordPress Rest发布后,元数据将自动序列化,不会显示任何图像。

代码语言:javascript
复制
"fave_property_images":["a:3:{i:0;s:5:\"18297\";i:1;s:5:\"18337\";i:2;s:5:\"17855\";}"]

是否有办法防止自动序列化我的数据?

编辑了修改了我的getcallback函数,并引用了https://developer.wordpress.org/reference/functions/register_休息_外地/

functions.php

代码语言:javascript
复制
function add_fave_property_images() {
    register_rest_field('property',
        'fave_property_images',
        array(
            'get_callback' => 'get_post_meta_for_api',
            'update_callback' => 'rest_update_property_meta',
            'schema' => array(
                                'type'  => 'array',
                                'items' => array(
                                    'type'   => 'string',
                                    ),
                            ),
        )
    );
}

function get_post_meta_for_api( $object ) {
    //return the post meta
    return get_post_meta( $object['id'], 'fave_property_images', false) ;
}

function rest_update_property_meta($value, $post, $field_name) {

  // Update the field
  return update_post_meta($post->ID, $field_name, $value);
}

我也尝试过maybe_unserialize(),但没有结果。我很感谢你的帮助,谢谢!

2nd编辑我目前正在使用postman发布仅仅是为了测试目的。这是我寄来的样本

代码语言:javascript
复制
{
  "status": "publish",
  "type": "property",
  "title": {
    "raw": "test"
  },
  "content": {
    "raw": "Singapore Management University (SMU) is the third autonomous university in Singapore. The university provides broad-based business programmes modelled after the Wharton School of the University of Pennsylvania. SMU was established on 29 July 2000 and is located in the Downtown area of Singapore. It has a city campus with a total enrolment of about 10,000 undergraduate and postgraduate students and comprises six schools offering undergraduate, graduate and PhD programmes in business administration, business analytics, financial services, accountancy, economics, information systems management, law and the social sciences. The university has over 30 research institutes and centres of excellence, and customised corporate training and lifelong learning for individuals are available through the university’s professional and executive development programmes. SMU is accredited by the AACSB International, EQUIS, and AMBA.\n",
    "protected": False
  },
  "excerpt": {
    "raw": "excerpt\n",
    "protected": False
  },
  "author": 1,
  "featured_media": 18297,
  "parent": 0,
  "menu_order": 0,
  "template": "",
  "property_type": [
    1072,
    1073,
    1074,
    1071
  ],
  "property_status": [],
  "property_feature": [
    1079,
    1080,
    1081,
    1082
  ],
  "property_label": [],
  "property_country": [
    1065
  ],
  "property_city": [
    1357
  ],

  "fave_qs-ranking": "50-100",
  "fave_percentage-employed-after-graduation": "90%",
  "fave_school-fees": "$23000",
  "fave_local-to-international-students": "1:1",
  "fave_ielts-toefl": "10:10",
  "fave_times_ranking": "3",
  "fave_minimum-gpa": "23",
  "fave_property_images": ["18297","18337","17855"],
  "fave_property_price": "$200",
  "fave_property_map_address": "81 Victoria St, Singapore 188065",
  "fave_property_address": "81 Victoria St",
  "fave_property_zip": "188065"

}

3rd编辑我已经设法在fave_property_images字段上显示了数组,但是property_meta中的字段仍然是序列化数组,这是/wp/wp/v2/school中生成的示例json。

代码语言:javascript
复制
{
  "id": 18397,
  "date": "2021-07-24T16:50:22",
  "date_gmt": "2021-07-24T08:50:22",
  "guid": {
    "rendered": "https://www.abroad.me/index.php/school/test/"
  },
  "modified": "2021-07-24T16:50:22",
  "modified_gmt": "2021-07-24T08:50:22",
  "slug": "test",
  "status": "publish",
  "type": "property",
  "link": "https://www.abroad.me/index.php/school/test/",
  "title": {
    "rendered": "test"
  },
  "content": {
    "rendered": "Singapore Management University (SMU) is the third autonomous university in Singapore. The university provides broad-based business programmes modelled after the Wharton School of the University of Pennsylvania. SMU was established on 29 July 2000 and is located in the Downtown area of Singapore. It has a city campus with a total enrolment of about 10,000 undergraduate and postgraduate students and comprises six schools offering undergraduate, graduate and PhD programmes in business administration, business analytics, financial services, accountancy, economics, information systems management, law and the social sciences. The university has over 30 research institutes and centres of excellence, and customised corporate training and lifelong learning for individuals are available through the university’s professional and executive development programmes. SMU is accredited by the AACSB International, EQUIS, and AMBA.\n",
    "protected": false
  },
  "excerpt": {
    "rendered": "excerpt\n",
    "protected": false
  },
  "author": 1,
  "featured_media": 18297,
  "parent": 0,
  "menu_order": 0,
  "template": "",
  "property_type": [
    1072,
    1073,
    1074,
    1071
  ],
  "property_status": [],
  "property_feature": [
    1079,
    1080,
    1081,
    1082
  ],
  "property_label": [],
  "property_country": [
    1065
  ],
  "property_city": [
    1357
  ],
  "property_meta": {
            "_thumbnail_id": [
                "18397"
            ],
            "_wp_page_template": [
                ""
            ],
            "fave_property_images": [
                "a:3:{i:0;s:5:\"18297\";i:1;s:5:\"18337\";i:2;s:5:\"17855\";}"
            ],
            "fave_minimum-gpa": [
                "23"
            ],
            "fave_property_price": [
                "$200"
            ],
            "fave_property_map_address": [
                "81 Victoria St, Singapore 188065"
            ],
            "fave_property_address": [
                "81 Victoria St"
            ],
            "fave_property_zip": [
                "188065"
            ],
            "fave_qs-ranking": [
                "50-100"
            ],
            "fave_percentage-employed-after-graduation": [
                "90%"
            ],
            "fave_school-fees": [
                "$23000"
            ],
            "fave_local-to-international-students": [
                "1:1"
            ],
            "fave_ielts-toefl": [
                "10:10"
            ]
        },
        "fave_property_images": [
            "18297",
            "18337",
            "17855"
        ],
        "fave_minimum-gpa": "23",
        "fave_times-ranking": "",
        "fave_property_year": "",
        "fave_property_price": "$200",
        "fave_property_location": "",
        "fave_houzez_geolocation_lat": "",
        "houzez_geolocation_long": "",
        "fave_property_map_address": "81 Victoria St, Singapore 188065",
        "fave_property_address": "81 Victoria St",
        "fave_property_zip": "188065",
        "fave_qs-ranking": "50-100",
        "fave_percentage-employed-after-graduation": "90%",
        "fave_school-fees": "$23000",
        "fave_local-to-international-students": "1:1",
        "fave_ielts-toefl": "10:10",
  "_links": {
    "self": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools/18397"
      }
    ],
    "collection": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools"
      }
    ],
    "about": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/types/property"
      }
    ],
    "author": [
      {
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/users/1"
      }
    ],
    "version-history": [
      {
        "count": 0,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools/18397/revisions"
      }
    ],
    "wp:featuredmedia": [
      {
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/media/18297"
      }
    ],
    "wp:attachment": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/media?parent=18397"
      }
    ],
    "wp:term": [
      {
        "taxonomy": "property_type",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_type?post=18397"
      },
      {
        "taxonomy": "property_status",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_status?post=18397"
      },
      {
        "taxonomy": "property_feature",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_feature?post=18397"
      },
      {
        "taxonomy": "property_label",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_label?post=18397"
      },
      {
        "taxonomy": "property_country",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_country?post=18397"
      },
      {
        "taxonomy": "property_city",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_city?post=18397"
      }
    ],
    "curies": [
      {
        "name": "wp",
        "href": "https://api.w.org/{rel}",
        "templated": true
      }
    ]
  }
}

但是,如果我通过编辑wordpress上的帖子来添加图像,json将如下所示,而property_meta下的字段D10将只是一个数组。

代码语言:javascript
复制
{
  "id": 18367,
  "date": "2021-07-23T11:13:46",
  "date_gmt": "2021-07-23T03:13:46",
  "guid": {
    "rendered": "https://www.abroad.me/index.php/school/test-8/"
  },
  "modified": "2021-07-23T14:52:49",
  "modified_gmt": "2021-07-23T06:52:49",
  "slug": "test-8",
  "status": "publish",
  "type": "property",
  "link": "https://www.abroad.me/index.php/school/test-8/",
  "title": {
    "rendered": "test"
  },
  "content": {
    "rendered": "Singapore Management University (SMU) is the third autonomous university in Singapore. The university provides broad-based business programmes modelled after the Wharton School of the University of Pennsylvania. SMU was established on 29 July 2000 and is located in the Downtown area of Singapore. It has a city campus with a total enrolment of about 10,000 undergraduate and postgraduate students and comprises six schools offering undergraduate, graduate and PhD programmes in business administration, business analytics, financial services, accountancy, economics, information systems management, law and the social sciences. The university has over 30 research institutes and centres of excellence, and customised corporate training and lifelong learning for individuals are available through the university’s professional and executive development programmes. SMU is accredited by the AACSB International, EQUIS, and AMBA.\n",
    "protected": false
  },
  "excerpt": {
    "rendered": "excerpt\n",
    "protected": false
  },
  "author": 1,
  "featured_media": 18297,
  "parent": 0,
  "menu_order": 0,
  "template": "",
  "property_type": [
    1072,
    1073,
    1074,
    1071
  ],
  "property_status": [],
  "property_feature": [
    1079,
    1080,
    1081,
    1082
  ],
  "property_label": [],
  "property_country": [
    1065
  ],
  "property_city": [
    1357
  ],
  "property_meta": {
    "_thumbnail_id": [
      "18297"
    ],
    "_wp_page_template": [
      ""
    ],
    "fave_percentage-employed-after-graduation": [
      "90%"
    ],
    "fave_school-fees": [
      "$23000"
    ],
    "fave_local-to-international-students": [
      "1:1"
    ],
    "fave_ielts-toefl": [
      "10:10"
    ],
    "fave_minimum-gpa": [
      "23"
    ],
    "fave_property_price": [
      "$200"
    ],
    "fave_property_map_address": [
      "81 Victoria St, Singapore 188065"
    ],
    "fave_property_address": [
      "81 Victoria St"
    ],
    "fave_property_zip": [
      "188065"
    ],
    "houzez_total_property_views": [
      "39"
    ],
    "houzez_views_by_date": [
      "a:2:{s:10:\"07-23-2021\";i:38;s:10:\"07-24-2021\";i:1;}"
    ],
    "houzez_recently_viewed": [
      "2021-07-24 12:15:46"
    ],
    "_edit_lock": [
      "1627030465:1"
    ],
    "_edit_last": [
      "1"
    ],
    "fave_property_location": [
      "1.2977432527616,103.84889449988,18"
    ],
    "houzez_geolocation_lat": [
      "1.2977432527616"
    ],
    "houzez_geolocation_long": [
      "103.84889449988"
    ],
    "fave_property_map_street_view": [
      "hide"
    ],
    "fave_agents": [
      "-1"
    ],
    "fave_property_agency": [
      "-1"
    ],
    "fave_single_top_area": [
      "global"
    ],
    "fave_single_content_area": [
      "global"
    ],
    "rs_page_bg_color": [
      ""
    ],
    "houzez_manual_expire": [
      "0"
    ],
    "_houzez_expiration_date_status": [
      "saved"
    ],
    "fave_property_map": [
      "1"
    ],
    "fave_property_images": [
      "18339",
      "18337",
      "18321",
      "17794",
      "17793",
      "17792",
      "17791",
      "17790",
      "17789",
      "17787"
    ]
  },
  "fave_qs-ranking": false,
  "fave_percentage-employed-after-graduation": false,
  "fave_school-fees": false,
  "fave_local-to-international-students": false,
  "fave_ielts-toefl": false,
  "fave_times_ranking": "",
  "fave_property_images": false,
  "fave_minimum-gpa": false,
  "fave_times-ranking": false,
  "fave_property_year": false,
  "fave_property_price": false,
  "fave_property_location": false,
  "fave_houzez_geolocation_lat": false,
  "houzez_geolocation_long": false,
  "fave_property_map_address": false,
  "fave_property_address": false,
  "fave_property_zip": false,
  "_links": {
    "self": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools/18367"
      }
    ],
    "collection": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools"
      }
    ],
    "about": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/types/property"
      }
    ],
    "author": [
      {
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/users/1"
      }
    ],
    "version-history": [
      {
        "count": 0,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/schools/18367/revisions"
      }
    ],
    "wp:featuredmedia": [
      {
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/media/18297"
      }
    ],
    "wp:attachment": [
      {
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/media?parent=18367"
      }
    ],
    "wp:term": [
      {
        "taxonomy": "property_type",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_type?post=18367"
      },
      {
        "taxonomy": "property_status",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_status?post=18367"
      },
      {
        "taxonomy": "property_feature",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_feature?post=18367"
      },
      {
        "taxonomy": "property_label",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_label?post=18367"
      },
      {
        "taxonomy": "property_country",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_country?post=18367"
      },
      {
        "taxonomy": "property_city",
        "embeddable": true,
        "href": "https://www.abroad.me/index.php/wp-json/wp/v2/property_city?post=18367"
      }
    ],
    "curies": [
      {
        "name": "wp",
        "href": "https://api.w.org/{rel}",
        "templated": true
      }
    ]
  }
}
EN

回答 1

WordPress Development用户

回答已采纳

发布于 2021-07-27 04:30:49

所以我找到了我自己的答案,fave_property_images不应该是数组,它只是多个字符串。因此,function.php中的以下代码解决了这个问题。

代码语言:javascript
复制
function add_fave_property_images() {
    register_rest_field('property',
        'fave_property_images',
        array(
            'get_callback' => 'get_property_images',
            'update_callback' => 'update_property_images',
        )
    );
}

function get_property_images( $object ) {
    //return the post meta
    return get_post_meta( $object['id'], 'fave_property_images', false);
}
function update_property_images($value, $post, $field_name) {
  // Update the field
    foreach($value as $ID){
        add_post_meta($post->ID, $field_name, $ID);
    }
    return;
  
}

add_action('rest_api_init', 'add_fave_property_images');
票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/392351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档