Benutzerdefinierter WP-API-Beitragstyp mit benutzerdefinierter Taxonomie

Lesezeit: 4 Minuten

Ich kämpfe mit https://github.com/WP-API/WP-API Ich versuche, einen benutzerdefinierten Beitragstyp mit einer benutzerdefinierten Taxonomie namens „listing_categy“ hinzuzufügen: Abfrage dieses Endpunkts:

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

gibt mir das:

[
  {
    "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"
      }
    }
  }
]

Die PHP-Datei, die das Posting verarbeitet, ist diese 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;
  }

Ich habe versucht, den json an die /posts zu senden, und es funktioniert gut für alle außer den Bedingungen ….

Ich habe folgendes versucht: das:

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

Dies:

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

Dies:

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

Dies:

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

die Dokumentation ist schrecklich

  • Hast du dazu eine Lösung gefunden? Ich habe das gleiche Problem.

    Benutzer1469270

    1. Dezember 2014 um 12:18 Uhr

  • Ich habe es eine Minute ausgegraben!

    – Vimes1984

    3. Dezember 2014 um 9:22 Uhr

Versuchen Sie es damit:
[your-site-root-dev-domain]/wp-json/posts/?type=[type-of-your-post]&Filter[cat]=35

Bitte werfen Sie einen Blick auf:
https://github.com/WP-API/WP-API/issues/343

Sag mir, ob dir das hilft.

  • Ihre Lösung besteht darin, sie zu finden, nicht zu posten.

    – PanMan

    30. Dezember 2014 um 8:15 Uhr

  • @PanMan Ja, und darüber ist die Frage. Bitte lesen Sie noch einmal.

    – drmartin

    14. März 2015 um 0:40 Uhr

Benutzer-Avatar
r0b3rt0

Bei “listing_categy” geht es um GET, hier haben Sie POSTing-Post-Parameter
Form WP-API API-Dokumente

Eine schnelle, aber schmutzige Lösung könnte darin bestehen, etwas Code hinzuzufügen einfügen_beitrag in lib/class-wp-json-posts.php.

In Zeile 921 einfügen:

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

und gleich danach

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

Einfügung

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
    }

Fügen Sie nun diese Eigenschaften zu Ihrem JSON-Post-Objekt hinzu, das so aussieht

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

*Dies muss mithilfe von Filtern umgestaltet werden

Ich habe die wp-api-Ressource verwendet https://github.com/jeffsebring/angular-wp-api Dann habe ich mein Objekt so gebaut:

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] } } },

1352290cookie-checkBenutzerdefinierter WP-API-Beitragstyp mit benutzerdefinierter Taxonomie

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy