具有自定义分类的WP-API自定义帖子类型

时间:2014-07-18 21:17:02

标签: json wordpress rest wordpress-plugin

我正在与https://github.com/WP-API/WP-API战斗我试图使用名为listing_categy的自定义分类添加自定义帖子类型: 查询此端点:

http://example.com/subfolder/wp-json/taxonomies/listing_categy/terms

给了我这个:

[
  {
    "ID": 9,
    "name": "buying",
    "slug": "buying-2",
    "description": "",
    "parent": null,
    "count": 1,
    "link": "http:\/\/example.com\/subfolder\/listing_categy\/buying-2\/",
    "meta": {
      "links": {
        "collection": "http:\/\/example.com\/subfolder\/wp-json\/taxonomies\/listing_categy\/terms",
        "self": "http:\/\/example.com\/subfolder\/wp-json\/taxonomies\/listing_categy\/terms\/7"
      }
    }
  },
  {
    "ID": 10,
    "name": "selling",
    "slug": "selling-2",
    "description": "",
    "parent": null,
    "count": 0,
    "link": "http:\/\/example.com\/subfolder\/listing_categy\/selling-2\/",
    "meta": {
      "links": {
        "collection": "http:\/\/example.com\/subfolder\/wp-json\/taxonomies\/listing_categy\/terms",
        "self": "http:\/\/example.com\/subfolder\/wp-json\/taxonomies\/listing_categy\/terms\/8"
      }
    }
  }
]

处理发布的php文件是https://github.com/WP-API/WP-API/blob/master/lib/class-wp-json-posts.php

  /**
   * Create a new post for any registered post type.
   *
   * @since 3.4.0
   * @internal 'data' is used here rather than 'content', as get_default_post_to_edit uses $_REQUEST['content']
   *
   * @param array $content Content data. Can contain:
   *  - post_type (default: 'post')
   *  - post_status (default: 'draft')
   *  - post_title
   *  - post_author
   *  - post_excerpt
   *  - post_content
   *  - post_date_gmt | post_date
   *  - post_format
   *  - post_password
   *  - comment_status - can be 'open' | 'closed'
   *  - ping_status - can be 'open' | 'closed'
   *  - sticky
   *  - post_thumbnail - ID of a media item to use as the post thumbnail/featured image
   *  - custom_fields - array, with each element containing 'key' and 'value'
   *  - terms - array, with taxonomy names as keys and arrays of term IDs as values
   *  - terms_names - array, with taxonomy names as keys and arrays of term names as values
   *  - enclosure
   *  - any other fields supported by wp_insert_post()
   * @return array Post data (see {@see WP_JSON_Posts::get_post})
   */
  public function new_post( $data ) {
    unset( $data['ID'] );

    $result = $this->insert_post( $data );
    if ( $result instanceof WP_Error ) {
      return $result;
    }

    $response = json_ensure_response( $this->get_post( $result ) );
    $response->set_status( 201 );
    $response->header( 'Location', json_url( '/posts/' . $result ) );

    return $response;
  }

我已经尝试将json发送到/ posts并且它适用于所有条款......

我已经尝试了以下所有方法: 这个:

"title": $scope.listobject.listname, "status": "publish", "slug": "selling","type": "listing", "post_meta": $scope.dataform, "terms": { "listing_categy": [9] }

这个:

"title": $scope.listobject.listname, "status": "publish", "slug": "selling","type": "listing", "post_meta": $scope.dataform, "terms": { "listing_categy": [ {"ID": 9 } ]} 

这个:

"title": $scope.listobject.listname, "status": "publish", "slug": "selling","type": "listing", "post_meta": $scope.dataform, "terms": { "listing_categy": [9, 10 ] } 

这:

"terms_names": { "listing_categy": ["selling", "buying"] } 

文档很糟糕

3 个答案:

答案 0 :(得分:6)

试试这个: ?[您现场根-DEV-域] /可湿性粉剂JSON /帖/类型= [类型的-您-交]安培;过滤[催化剂] = 35

请查看: https://github.com/WP-API/WP-API/issues/343

告诉我这是否对您有帮助。

答案 1 :(得分:0)

" listing_categy"是关于GET,这里你有posting post参数 form WP-API api docs

一个快速而又糟糕的解决方案可能是在lib / class-wp-json-posts.php中为 insert_post 添加一些代码。

在第921行插入:

if( ! empty($data['term']))
    {
        $post_custom_tax['term']=$data['term'];
    }
    if( ! empty($data['tax']))
    {
        $post_custom_tax['tax']=$data['tax'];
    }

之后

$post_ID = $update ? wp_update_post( $post, true ) : wp_insert_post( $post, true );

插入

if(!empty($post_custom_tax['term']) && ! empty( $post_custom_tax['tax']))
    {
        wp_set_object_terms( $post_ID, $post_custom_tax['term'], $post_custom_tax['tax'], true ); //with true for appending categories
    }

现在将此属性添加到您的json post对象,看起来像

"term": "custom-term-slug",
"tax": "custom-tax-slug"

*这需要使用过滤器重构

答案 2 :(得分:-1)

我使用了wp-api资源https://github.com/jeffsebring/angular-wp-api然后我构建了我的对象:

wpAPIResource.save({ param1: 'posts' },{ "title": $scope.listobject.listname, "slug": $scope.listobject.typeoflist,"type": "listing", "post_meta": $scope.dataform,  "tax_input": { "listing_categy": { "term_names": [$scope.listobject.typeoflist] } } },